Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将十六进制转换为字符串?_C#_Hex - Fatal编程技术网

C# 如何将十六进制转换为字符串?

C# 如何将十六进制转换为字符串?,c#,hex,C#,Hex,我有一些十六进制数据: 48|65|6c|6c|6f|20|53|68|61|72|6f|6b|2e| 20|3d|43|46|3d|46|30|3d|45|38|3d|45|32|3d|45|35|3d|46|32|0d|0a|0d|0a|2e|0d|0a| 第一个文本字符串是“Hello Sharok”(不带引号)。第二个文本字符串是“ППцццццц”(俄语中没有引号,“ПППцццццц。如何将其转换为可读文本(第一个字符串正常,第二个字符串失败) 代码页:Windows-1251(

我有一些十六进制数据:

48|65|6c|6c|6f|20|53|68|61|72|6f|6b|2e|

20|3d|43|46|3d|46|30|3d|45|38|3d|45|32|3d|45|35|3d|46|32|0d|0a|0d|0a|2e|0d|0a|
第一个文本字符串是“Hello Sharok”(不带引号)。第二个文本字符串是“ППцццццц”(俄语中没有引号,“ПППцццццц。如何将其转换为可读文本(第一个字符串正常,第二个字符串失败)

代码页:Windows-1251()

请查看此处

这是有用的

看看这里

这是有用的


为windows-1251编码创建一个
编码
对象,并解码字节数组:

byte[] data = {
  0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x53, 0x68, 0x61, 0x72, 0x6f, 0x6b, 0x2e
};

string text = Encoding.GetEncoding(1251).GetString(data);
第二组数据不解码为俄语字符,而是解码为俄语字符(包括开始处的空格和结束三行的换行符(CR+LF)):

要获得所需的字符串,首先必须将数据解码为字符串,然后从字符串中提取十六进制代码,将其转换为字节,然后解码这些字节:

Encoding win = Encoding.GetEncoding(1251);
string text = win.GetString(
  Regex.Matches(win.GetString(data), "=(..)")
  .OfType<Match>()
  .Select(m => Convert.ToByte(m.Groups[1].Value, 16))
  .ToArray()
);
Encoding win=Encoding.GetEncoding(1251);
string text=win.GetString(
Regex.Matches(win.GetString(数据),“=(..”)
第()类
.Select(m=>Convert.ToByte(m.Groups[1].Value,16))
.ToArray()
);

为windows-1251编码创建一个
编码
对象,并解码字节数组:

byte[] data = {
  0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x53, 0x68, 0x61, 0x72, 0x6f, 0x6b, 0x2e
};

string text = Encoding.GetEncoding(1251).GetString(data);
第二组数据不解码为俄语字符,而是解码为俄语字符(包括开始处的空格和结束三行的换行符(CR+LF)):

要获得所需的字符串,首先必须将数据解码为字符串,然后从字符串中提取十六进制代码,将其转换为字节,然后解码这些字节:

Encoding win = Encoding.GetEncoding(1251);
string text = win.GetString(
  Regex.Matches(win.GetString(data), "=(..)")
  .OfType<Match>()
  .Select(m => Convert.ToByte(m.Groups[1].Value, 16))
  .ToArray()
);
Encoding win=Encoding.GetEncoding(1251);
string text=win.GetString(
Regex.Matches(win.GetString(数据),“=(..”)
第()类
.Select(m=>Convert.ToByte(m.Groups[1].Value,16))
.ToArray()
);

第二个字符串不是Windows-1251,而是“
=CF=F0=E8=E2=E5=F2.
”,其中的解码字符实际上是Windows-1251。因此,您需要迭代字符串,并逐个字符构建输出字符串。若遇到转义符号(=),则下两个字符是Windows-1251的十六进制数字。解码两个数字并将结果字符添加到输出字符串中。循环直到结束。

第二个字符串不是Windows-1251,而是“
=CF=F0=E8=E2=E5=F2.
”,其中的解码字符实际上是Windows-1251。因此,您需要迭代字符串,并逐个字符构建输出字符串。若遇到转义符号(=),则下两个字符是Windows-1251的十六进制数字。解码两个数字并将结果字符添加到输出字符串中。循环直到结束。

对于第二个循环,您可以使用:

string input="20|3d|43|46|3d|46|30|3d|45|38|3d|45|32|3d|45|35|3d|46|32|0d|0a|0d|0a|2e|0d|0a";
byte[] bytes=input.Split('|').Select(s=>byte.Parse(s, System.Globalization.NumberStyles.HexNumber)).ToArray();
string text = Encoding.GetEncoding(1251).GetString(bytes);

StringBuilder text2=new StringBuilder();
for(int i=0;i<text.Length;i++)
{
  if (text[i]=='=')
  {
    string hex=text[i+1].ToString()+text[i+2].ToString();
    byte b=byte.Parse(hex, System.Globalization.NumberStyles.HexNumber);

    text2.Append(Encoding.GetEncoding(1251).GetString(new byte[]{b}));
    i+=2;
  }
  else
  {
    text2.Append(text[i]);
  }
}
string input=“20 | 3d | 43 | 3d | 46 | 30 | 3d | 45 | 38 | 3d | 45 | 3d | 46 | 32 | 0d | 0a | 2e | 0d 1240A”;
byte[]bytes=input.Split('|')。选择(s=>byte.Parse(s,System.Globalization.NumberStyles.HexNumber)).ToArray();
字符串文本=Encoding.GetEncoding(1251).GetString(字节);
StringBuilder text2=新的StringBuilder();

对于(int i=0;i对于第二个,您可以使用:

string input="20|3d|43|46|3d|46|30|3d|45|38|3d|45|32|3d|45|35|3d|46|32|0d|0a|0d|0a|2e|0d|0a";
byte[] bytes=input.Split('|').Select(s=>byte.Parse(s, System.Globalization.NumberStyles.HexNumber)).ToArray();
string text = Encoding.GetEncoding(1251).GetString(bytes);

StringBuilder text2=new StringBuilder();
for(int i=0;i<text.Length;i++)
{
  if (text[i]=='=')
  {
    string hex=text[i+1].ToString()+text[i+2].ToString();
    byte b=byte.Parse(hex, System.Globalization.NumberStyles.HexNumber);

    text2.Append(Encoding.GetEncoding(1251).GetString(new byte[]{b}));
    i+=2;
  }
  else
  {
    text2.Append(text[i]);
  }
}
string input=“20 | 3d | 43 | 3d | 46 | 30 | 3d | 45 | 38 | 3d | 45 | 3d | 46 | 32 | 0d | 0a | 2e | 0d 1240A”;
byte[]bytes=input.Split('|')。选择(s=>byte.Parse(s,System.Globalization.NumberStyles.HexNumber)).ToArray();
字符串文本=Encoding.GetEncoding(1251).GetString(字节);
StringBuilder text2=新的StringBuilder();
对于(int i=0;i