将 unicode 字符的十六进制序列解码为字符串的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/371096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to decode hex sequence of unicode characters to string
提问by Valentin Vasilyev
What is the most code free way to decode a string:
解码字符串的最无代码方法是什么:
\xD0\xAD\xD0\xBB\xD0\xB5\xD0\xBA\xD1\x82\xD1\x80\xD0\xBE\xD0\xBD\xD0\xBD\xD0\xB0\xD1\x8F
to human string in C#?
到 C# 中的人字串?
This hex string contains some unicode symbols.
这个十六进制字符串包含一些 unicode 符号。
I know about
我知道
System.Convert.ToByte(string, fromBase);
But I was wondering if there are some built-in helpers that asp.net internally uses.
但我想知道是否有一些 asp.net 内部使用的内置助手。
采纳答案by gimel
In this site you are not likely to get a code free way(it's about code). Decoding a hex encoded byte arrayis possible if you know the original encoding.
在此站点中,您不太可能获得无代码方式(这是关于代码的)。如果您知道原始编码,则可以解码十六进制编码的字节数组。
Guessing the encoding to be UTF8, decoding it with System.Text.UTF8encodingyields the following 11 unicode characters Cyrillic string:
猜测编码为 UTF8,使用System.Text.UTF8encoding对其进行解码会 产生以下 11 个 unicode 字符Cyrillic string:
CYRILLIC CAPITAL LETTER E, CYRILLIC SMALL LETTER EL, CYRILLIC SMALL LETTER IE, CYRILLIC SMALL LETTER KA, CYRILLIC SMALL LETTER TE, CYRILLIC SMALL LETTER ER, CYRILLIC SMALL LETTER O, CYRILLIC SMALL LETTER EN, CYRILLIC SMALL LETTER EN, CYRILLIC SMALL LETTER A, CYRILLIC SMALL LETTER YA,
西里尔大写字母 E、西里尔小写字母 EL、西里尔小写字母 IE、西里尔小写字母 KA、西里尔小写字母 TE、西里尔小写字母 ER、西里尔小写字母 E、小写字母 L、小写字母 L西里尔小写字母 YA,
Once you figure how to get your data into a Byte[]
,
the sample code in the above reference shows the way:
一旦您弄清楚如何将数据放入Byte[]
,上述参考中的示例代码显示了以下方式:
// fill encodedBytes with original data
Byte[] encodedBytes = new Byte[] {0xD0,0xAD,0xD0,0xBB,0xD0,0xB5}; //...
UTF8Encoding utf8 = new UTF8Encoding();
String decodedString = utf8.GetString(encodedBytes);