在VB.NET中将UTF-8转换为windows-1255编码

时间:2013-04-28 14:21:52

标签: vb.net character-encoding

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

提前致谢。

1 个答案:

答案 0 :(得分:0)

我修改了您的代码。 将文本从一种编码转换为另一种编码非常简单。 这就是您应该在VB.Net中执行的操作。 Microsof 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