.NET Convert from string of Hex values into Unicode characters (Support different code pages)

早过忘川 提交于 2019-12-01 23:15:39

问题


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!


回答1:


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);



回答2:


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));
   }
} 


来源:https://stackoverflow.com/questions/8608489/net-convert-from-string-of-hex-values-into-unicode-characters-support-differen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!