Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 将字符从CP437编码转换为UTF-8编码总是产生相同的字符代码,因此不是相同的字符 问题_Vb.net_Encoding_Utf 8_Character Encoding_Codepage 437 - Fatal编程技术网

Vb.net 将字符从CP437编码转换为UTF-8编码总是产生相同的字符代码,因此不是相同的字符 问题

Vb.net 将字符从CP437编码转换为UTF-8编码总是产生相同的字符代码,因此不是相同的字符 问题,vb.net,encoding,utf-8,character-encoding,codepage-437,Vb.net,Encoding,Utf 8,Character Encoding,Codepage 437,我正在尝试将字符和/或字节数组从转换为UTF-8()。问题是,无论我尝试什么,代码总是生成相同的字符代码,但由于两种编码具有不同的字符集映射到字符代码,因此生成的字符不相同 作为一个例子,我正在尝试从CP437转换字符代码为3的字符(心:♥)到UTF-8,我仍然希望它是相同的字符。但是,当转换为UTF-8时,它仍然使用字符代码3,这导致一个名为的控制字符(请参阅以获取字符列表) 我的尝试 以下是我的一些尝试: (通用代码) 使用: Dim ConvertedBytes As Byte() =

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

作为一个例子,我正在尝试从CP437转换字符代码为3的字符(心:
)到UTF-8,我仍然希望它是相同的字符。但是,当转换为UTF-8时,它仍然使用字符代码3,这导致一个名为的控制字符(请参阅以获取字符列表)


我的尝试 以下是我的一些尝试:

(通用代码)

使用:

Dim ConvertedBytes As Byte() = Encoding.Convert(CP437, Encoding.UTF8, BytesToConvert)
DebugEncodedArray(ConvertedBytes, Encoding.UTF8)
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)

使用,使用特定编码写入:

Dim ConvertedBytes As Byte() = Encoding.Convert(CP437, Encoding.UTF8, BytesToConvert)
DebugEncodedArray(ConvertedBytes, Encoding.UTF8)
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)

写入文件,然后读取并转换字节(对于我需要这段代码的目的来说不是最佳选择):

Dim ConvertedBytes As Byte() = Encoding.Convert(CP437, Encoding.UTF8, BytesToConvert)
DebugEncodedArray(ConvertedBytes, Encoding.UTF8)
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
传递给
DebugEncodedArray()
而不是
Encoding.UTF8


预期结果 我期待的结果是:

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


有关于我做错了什么的线索吗?

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

(供未来读者参考)这就是我最终使用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 _
    }

低范围是上下文的,我认为您已经证明,对于1-31和127,您将需要一个简单的查找,因为.Net是在控制代码上下文而不是图形上下文中解释它们。(即
0xA
)是
\n
不是该图形的等效unicode代码点)@AlexK:嗯,没想到。。。它可以将它们解释为控制字符,因此不会重新映射它们那么使用查找表安全吗?我的意思是,即使在将来,这些字符在UTF-8中也会有相同的代码吗?(这可能是一个愚蠢的问题,但我不太了解字符编码及其所有规格或可能的更改)当然,如果您决定在看到0xA时,您将进入查找和输出◙ 那很好。当然,这意味着你将无法在文本中添加新行…@AlexK:那不成问题。charcode是以编程方式编写的,并且只应以UTF-8友好格式从CP437输出单个可显示字符。请将您的信息添加到答案中,以便我将其标记为已接受!非常感谢你的帮助!展示我如何利用你的建议解决我的问题。:)