1

我正在尝试将 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),和/或我在上面的代码中做错了什么?

提前致谢。

4

1 回答 1

0

我修改了你的代码。将文本从一种编码转换为另一种编码非常简单。这就是您应该在 VB.Net 中执行此操作的方式。Microsoft Windows 文件编码是 1252,而不是 1255。

    Public Function Utf82Hebrew(ByVal Str As String) As String
    Dim ascii As System.Text.Encoding = System.Text.Encoding.GetEncoding("1252")
    Dim unicode As System.Text.Encoding = System.Text.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() = System.Text.Encoding.Convert(unicode, ascii, unicodeBytes)

    ' Convert the new byte array into a char array and then into a string. 
    Dim asciiString As String = ascii.GetString(asciiBytes)

    Utf82Hebrew = asciiString
End Function
于 2020-12-09T18:45:32.917 回答