.NET 将十六进制值字符串转换为 Unicode 字符(支持不同的代码页)

发布于 2024-12-22 16:01:30 字数 802 浏览 5 评论 0原文

我有一串十六进制值...

String hexString = "8A65";

我需要将此字符串转换为其 Unicode 等效值。棘手的部分是我需要支持不同的代码页,有些代码页有“8A65”=一个字符,而其他代码页会将其转换为两个字符。

在需要执行转换之前,我事先不知道将使用哪个代码页。

我尝试过各种方法,例如

byte[] original = Encoding.Unicode.GetBytes(hexString);
byte[] conv= Encoding.Convert(Encoding.Unicode, Encoding.GetEncoding(932), orig);
char[] chars = Encoding.GetEncoding(932).GetChars(conv);

注意:代码页 932 是日语

解决方案

string hexString = "8A65";
int length = hexString.length;
byte[] bytes = new byte[length / 2];

for (int i = 0; i < length; i += 2)
{
    bytes[i / 2] = Convert.ToByte(hexString.Substring(i, 2), 16);
}

char[] chars = Encoding.GetEncoding(932).GetChars(bytes);

谢谢 pstrjds,你是救星!

I have a string of Hex values...

String hexString = "8A65";

I need to convert this string into their Unicode equivalents. The tricky part is that I need to support different code pages and some code pages have '8A65' = one character whereas other code pages would convert it into two characters.

I have no prior knowledge of which code page I will be using until I need to perform the conversion.

I've tried all sorts of stuff such as

byte[] original = Encoding.Unicode.GetBytes(hexString);
byte[] conv= Encoding.Convert(Encoding.Unicode, Encoding.GetEncoding(932), orig);
char[] chars = Encoding.GetEncoding(932).GetChars(conv);

Note: code page 932 is Japanese

SOLUTION

string hexString = "8A65";
int length = hexString.length;
byte[] bytes = new byte[length / 2];

for (int i = 0; i < length; i += 2)
{
    bytes[i / 2] = Convert.ToByte(hexString.Substring(i, 2), 16);
}

char[] chars = Encoding.GetEncoding(932).GetChars(bytes);

Thank you pstrjds, you are a life saver!

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

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

发布评论

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

评论(2

微暖i 2024-12-29 16:01:30

您需要将十六进制字符串转换为字节(请参阅SO 帖子)。将十六进制字符串传递到其中一种编码中以将其转换为字节只会为您提供这些字符的等效字节。我假设您想要的是 4 个字符串代表的两个字节,因此将十六进制解码为字节,然后您可以使用解码字节上的编码来取回字符串。

Encoding.{YourEncoding}.GetChars(hexBytes);

You need to convert the hex string to bytes (see SO post). Passing the hex string into one of the encodings to convert it to bytes just gives you the byte equivalent of those characters. I am assuming what you want is the two bytes that the 4 character string represents, so decode the hex to bytes and then you can use the encoding on the decoded bytes to get back a string.

Encoding.{YourEncoding}.GetChars(hexBytes);
落叶缤纷 2024-12-29 16:01:30
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

class Sample {
    static public void Main(){
        var data = "8A65";
        Regex regex = new Regex(@"(?<hex>[0-9A-F]{2})",RegexOptions.IgnoreCase | RegexOptions.Compiled);
        byte[] bytes = regex.Matches(data).OfType<Match>().Select(m => Convert.ToByte(m.Groups["hex"].Value,16)).ToArray();
        char[] chars = Encoding.GetEncoding(932).GetChars(bytes);
        Console.WriteLine(new String(chars));
   }
} 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

class Sample {
    static public void Main(){
        var data = "8A65";
        Regex regex = new Regex(@"(?<hex>[0-9A-F]{2})",RegexOptions.IgnoreCase | RegexOptions.Compiled);
        byte[] bytes = regex.Matches(data).OfType<Match>().Select(m => Convert.ToByte(m.Groups["hex"].Value,16)).ToArray();
        char[] chars = Encoding.GetEncoding(932).GetChars(bytes);
        Console.WriteLine(new String(chars));
   }
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文