VB.NET编码一个字符错误

发布于 2024-09-05 12:56:03 字数 766 浏览 3 评论 0原文

我有一个字节数组,正在将其编码为字符串:

Private Function GetKey() As String
    Dim ba() As Byte = {&H47, &H43, &H44, &H53, &H79, &H73, &H74, &H65, &H6D, _
                        &H73, &H89, &HA, &H1, &H32, &H31, &H36}

    Dim strReturn As String = Encoding.ASCII.GetString(ba)

    Return strReturn
End Function

然后我通过 IO.File.AppendAllText 将其写入文件。 如果我在 010 Editor 中打开该文件(以查看二进制数据),它将显示如下:

47 43 44 53 79 73 74 65 6D 73 3F 0A 01 32 31 36

原始字节数组包含 89 在位置 11,编码字符串包含 3F。 如果我将编码更改为 Encoding.Default.GetString,它会给我:

47 43 44 53 79 73 74 65 6D 73 E2 80 B0 0A 01 32 31 36

任何帮助将不胜感激!

I have a byte array that I'm encoding to a string:

Private Function GetKey() As String
    Dim ba() As Byte = {&H47, &H43, &H44, &H53, &H79, &H73, &H74, &H65, &H6D, _
                        &H73, &H89, &HA, &H1, &H32, &H31, &H36}

    Dim strReturn As String = Encoding.ASCII.GetString(ba)

    Return strReturn
End Function

Then I write that to a file via IO.File.AppendAllText.
If I open that file in 010 Editor (to view the binary data) it displays as this:

47 43 44 53 79 73 74 65 6D 73 3F 0A 01 32 31 36

The original byte array contained 89 at position 11, and the encoded string contains 3F.
If I change my encoding to Encoding.Default.GetString, it gives me:

47 43 44 53 79 73 74 65 6D 73 E2 80 B0 0A 01 32 31 36

Any help would be much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我早已燃尽 2024-09-12 12:56:03

Encoding.ASCII 限制为 7-位字符。即从 0 到 127(&H00 到 &H7F)的字节值。 GetString 将所有超出此范围的值设置为 &H3F(这是一个问号)。

Encoding.Default 是当前的 ANSI 代码我的计算机上的操作系统页面是 CodePage 1252..

ANSI 代码页可以不同
在不同的计算机上,或者可以
更改为单台计算机,领先
导致数据损坏。对于大多数
一致的结果、应用
应使用 Unicode,例如 UTF-8
(代码页 65001)或 UTF-16
特定代码页的。

Encoding.UTF7 在这里适合你:

Dim strReturn As String = Encoding.UTF7.GetString(ba)

编辑:

我不使用编码,而是直接使用如下方式写入字节:

Dim key = GetKey()
Dim f = System.IO.File.OpenWrite("output.txt")
f.Seek(0, SeekOrigin.End)
f.Write(key, 0, key.Length)
f.Close()

Private Function GetKey() As String
  Dim ba() As Byte = {&H47, &H43, &H44, &H53, &H79, &H73, &H74, &H65, &H6D, &H73, &H89, &HA, &H1, &H32, &H31, &H36}

  Return ba
End Function

Encoding.ASCII is limited to 7-bit characters. That is byte values from 0 to 127 (&H00 to &H7F). GetString sets all values outside this range to &H3F which is a questionmark.

Encoding.Default is the current ANSI code page for the operating system which on my computer is CodePage 1252..

The ANSI code pages can be different
on different computers, or can be
changed for a single computer, leading
to data corruption. For the most
consistent results, applications
should use Unicode, such as UTF-8
(code page 65001) or UTF-16, instead
of a specific code page.

Encoding.UTF7 would work for you here:

Dim strReturn As String = Encoding.UTF7.GetString(ba)

Edit:

Instead of using Encoding I'd write the bytes directly using something like this:

Dim key = GetKey()
Dim f = System.IO.File.OpenWrite("output.txt")
f.Seek(0, SeekOrigin.End)
f.Write(key, 0, key.Length)
f.Close()

Private Function GetKey() As String
  Dim ba() As Byte = {&H47, &H43, &H44, &H53, &H79, &H73, &H74, &H65, &H6D, &H73, &H89, &HA, &H1, &H32, &H31, &H36}

  Return ba
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文