将char从CP437编码转换为UTF-8编码始终产生相同的字符代码,因此不是相同的字符

时间:2017-07-28 14:18:58

标签: vb.net encoding utf-8 character-encoding codepage-437

问题

我正在尝试将字符和/或字节数组从CP437 encoding转换为UTF-8(Encoding.UTF8)。问题在于,无论我尝试什么,代码总是产生相同的字符代码,但由于这两个编码具有映射到字符代码的不同字符集,因此生成的字符不相同。

作为一个例子,我正在尝试将带有字符代码3的字符从CP437(一个心脏:)转换为UTF-8,我仍然希望它是相同的字符。但是,当转换为UTF-8时,它仍然使用字符代码3,这会产生一个名为ETX的控制字符(有关字符列表,请参阅UTF-8's codepage layout)。


我的尝试

以下是我的一些尝试:

(一般代码)

Public Shared ReadOnly CP437 As Encoding = Encoding.GetEncoding("IBM437")
Public Shared ReadOnly BytesToConvert As Byte() = New Byte(3 - 1) {3, 4, 5} 'Characters: ♥, ♦, ♣.

Public Sub DebugEncodedArray(ByVal Bytes As Byte(), ByVal Encoding As Encoding)
    Dim ResultingString As String = Encoding.GetString(Bytes)
    MessageBox.Show( _
            String.Format("Encoding: {1}{0}" & _
                          "String: ""{2}""{0}" & _
                          "Bytes: {Encoding.Convert()}{0}", _
                          Environment.NewLine, _
                          Encoding.EncodingName, _
                          ResultingString, _
                          String.Join(", ", Bytes)), _
        "Debug", MessageBoxButtons.OK, MessageBoxIcon.Information _
    )
End Sub

使用StreamWriter

Dim ConvertedBytes As Byte() = Encoding.Convert(CP437, Encoding.UTF8, BytesToConvert)
DebugEncodedArray(ConvertedBytes, Encoding.UTF8)


使用MemoryStream,使用特定编码写入UTF-8 result

Using MStream As New MemoryStream(16)
    Using Writer As New StreamWriter(MStream, CP437)
        Writer.Write(CP437.GetChars(BytesToConvert))
    End Using

    Dim UTF8Bytes As Byte() = Encoding.Convert(CP437, Encoding.UTF8, MStream.ToArray())
    DebugEncodedArray(UTF8Bytes, Encoding.UTF8)
End Using


写入文件,然后读取并转换字节(不是我需要此代码的最佳选择):

File.WriteAllText("C:\Users\Vincent\Desktop\test.txt", CP437.GetString(BytesToConvert), CP437)

Dim FileBytes As Byte() = File.ReadAllBytes("C:\Users\Vincent\Desktop\test.txt")
Dim UTF8Bytes As Byte() = Encoding.Convert(CP437, Encoding.UTF8, FileBytes)

DebugEncodedArray(UTF8Bytes, Encoding.UTF8)


结果

以上所有尝试都会产生相同的结果:

CP437 result

以及如果我将CP437传递给DebugEncodedArray()而不是Encoding.UTF8

Expected UTF-8 result


预期结果

我期待的结果是:

Dim UTF8Bytes As Byte() = Encoding.UTF8.GetBytes("♥♦♣")
DebugEncodedArray(UTF8Bytes, Encoding.UTF8)

C# CSOM Sharepoint Bearer request from azure active directory

关于我做错的任何线索?

2 个答案:

答案 0 :(得分:2)

CP437的低范围是背景。我认为你已经证明了1-31& 127你将需要一个简单的查找,因为.Net在控制代码上下文中解释它们而不是在图形上下文中 - 即◙(0xA)是\n而不是该图形的等效Unicode代码点

答案 1 :(得分:1)

(未来读者参考)这就是我最终使用Alex K.的建议解决我的问题的方法:

Dim Heart As Char = Convert.ToChar(CP437LookupTable(3)) 'Results in: ♥. YAY!

查找表:

'Lookup table for Codepage 437-to-Unicode character codes.
Private Shared ReadOnly CP437LookupTable As Integer() = _
    New Integer(256 - 1) { _
        0, 9786, 9787, 9829, 9830, 9827, 9824, _
        8226, 9688, 9675, 9689, 9794, 9792, 9834, 9835, _
        9788, 9658, 9668, 8597, 8252, 182, 167, 9644, _
        8616, 8593, 8595, 8594, 8592, 8735, 8596, 9650, _
        9660, 32, 33, 34, 35, 36, 37, 38, _
        39, 40, 41, 42, 43, 44, 45, 46, _
        47, 48, 49, 50, 51, 52, 53, 54, _
        55, 56, 57, 58, 59, 60, 61, 62, _
        63, 64, 65, 66, 67, 68, 69, 70, _
        71, 72, 73, 74, 75, 76, 77, 78, _
        79, 80, 81, 82, 83, 84, 85, 86, _
        87, 88, 89, 90, 91, 92, 93, 94, _
        95, 96, 97, 98, 99, 100, 101, 102, _
        103, 104, 105, 106, 107, 108, 109, 110, _
        111, 112, 113, 114, 115, 116, 117, 118, _
        119, 120, 121, 122, 123, 124, 125, 126, _
        8962, 199, 252, 233, 226, 228, 224, 229, _
        231, 234, 235, 232, 239, 238, 236, 196, _
        197, 201, 230, 198, 244, 246, 242, 251, _
        249, 255, 214, 220, 162, 163, 165, 8359, _
        402, 225, 237, 243, 250, 241, 209, 170, _
        186, 191, 8976, 172, 189, 188, 161, 171, _
        187, 9617, 9618, 9619, 9474, 9508, 9569, 9570, _
        9558, 9557, 9571, 9553, 9559, 9565, 9564, 9563, _
        9488, 9492, 9524, 9516, 9500, 9472, 9532, 9566, _
        9567, 9562, 9556, 9577, 9574, 9568, 9552, 9580, _
        9575, 9576, 9572, 9573, 9561, 9560, 9554, 9555, _
        9579, 9578, 9496, 9484, 9608, 9604, 9612, 9616, _
        9600, 945, 223, 915, 960, 931, 963, 181, _
        964, 934, 920, 937, 948, 8734, 966, 949, _
        8745, 8801, 177, 8805, 8804, 8992, 8993, 247, _
        8776, 176, 8729, 183, 8730, 8319, 178, 9632, _
        160 _
    }