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

16,520

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

Solution 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));
   }
} 
Share:
16,520
user1002479
Author by

user1002479

Updated on June 09, 2022

Comments

  • user1002479
    user1002479 almost 2 years

    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!