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

后端 未结 2 790
时光说笑
时光说笑 2020-12-19 15:01

I have a string of Hex values...

String hexString = \"8A65\";

I need to convert this string into their Unicode equivalents. The tricky part

2条回答
  •  被撕碎了的回忆
    2020-12-19 15:45

    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(@"(?[0-9A-F]{2})",RegexOptions.IgnoreCase | RegexOptions.Compiled);
            byte[] bytes = regex.Matches(data).OfType().Select(m => Convert.ToByte(m.Groups["hex"].Value,16)).ToArray();
            char[] chars = Encoding.GetEncoding(932).GetChars(bytes);
            Console.WriteLine(new String(chars));
       }
    } 
    

提交回复
热议问题