我正在尝试将 UTF-8 编码的字符串转换为 VB.NET 中的 windows-1255,但没有成功。诚然,我不了解 VB,但尝试在 MSDN 上使用示例并根据我的需要对其进行修改:
Public Function Utf82Hebrew(ByVal Str As String) As String
Dim ascii As Encoding = Encoding.GetEncoding("windows-1255")
Dim unicode As Encoding = Encoding.Unicode
' Convert the string into a byte array.
Dim unicodeBytes As Byte() = unicode.GetBytes(Str)
' Perform the conversion from one encoding to the other.
Dim asciiBytes As Byte() = Encoding.Convert(unicode, ascii, unicodeBytes)
' Convert the new byte array into a char array and then into a string.
Dim asciiChars(ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)-1) As Char
ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0)
Dim asciiString As New String(asciiChars)
Utf82Hebrew = asciiString
End Function
这个函数实际上并没有做任何事情——字符串仍然是 UTF-8。但是,如果我更改此行:
Dim ascii As Encoding = Encoding.GetEncoding("windows-1255")
对此:
Dim ascii As Encoding = Encoding.ASCII
然后该函数在字符串的位置返回问号。
有谁知道如何正确地将 UTF-8 字符串转换为特定编码(在本例中为 windows-1255),和/或我在上面的代码中做错了什么?
提前致谢。