本文介绍了C#中转换成十六进制值,UTF8和ASCII的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转换为十六进制值的字符串中两个ASCII值和UTF8价值。但是,当我执行下面的代码,它打印出相同的十六进制值,我给作为输入

 字符串十六进制串=68656c6c6f2c206d79206e616d6520697320796f752e; 
System.Text.UTF8Encoding编码=新System.Text.UTF8Encoding();
字节[] = dBytes encoding.GetBytes(十六进制串);

//要获得十六进制字符串的ASCII值。
串ASCIIresult = System.Text.Encoding.ASCII.GetString(dBytes);
MessageBox.Show(ASCIIresult,显示在ASCII值);

//要获得十六进制字符串
串utf8result = System.Text.Encoding.UTF8.GetString(dBytes)的UTF8价值;
MessageBox.Show(utf8result,显示在UTF8值);


解决方案

既然你命名一个变量十六进制串,我认为值已经编码成十六进制格式



这意味着下面的不会起作用:

 字符串十六进制串=68656c6c6f2c206d79206e616d6520697320796f752e; 
System.Text.UTF8Encoding编码=新System.Text.UTF8Encoding();
字节[] = dBytes encoding.GetBytes(十六进制串);

这是因为你是治疗已编码的字符串作为纯UTF8文本。



您很可能缺少一个步骤,以十六进制编码字符串转换成字节数组转换。



您可以在此使用的这说明此功能:

 公共静态的byte [] StringToByteArray(十六进制字符串)
{
INT NumberChars = hex.Length / 2;
字节[]字节=新的字节[NumberChars]
使用(VAR SR =新StringReader(十六进制))
{
的for(int i = 0; I< NumberChars;我++)
字节[i] =
Convert.ToByte(新的字符串(新的char [2] {(char)的sr.Read(),(炭)sr.Read()}),16);
}
返回字节;
}



所以,最终的结果会是这样的:

 字节[] = dBytes StringToByteArray(十六进制串); 

//要获得十六进制字符串的ASCII值。
串ASCIIresult = System.Text.Encoding.ASCII.GetString(dBytes);
MessageBox.Show(ASCIIresult,显示在ASCII值);

//要获得十六进制字符串
串utf8result = System.Text.Encoding.UTF8.GetString(dBytes)的UTF8价值;
MessageBox.Show(utf8result,显示在UTF8值);


I am trying to convert a hexadecimal values in a string in to both ASCII value and UTF8 value. But when I execute the following code, it prints out the same hex value I give as input

string hexString = "68656c6c6f2c206d79206e616d6520697320796f752e";
System.Text.UTF8Encoding  encoding=new System.Text.UTF8Encoding();
byte[] dBytes = encoding.GetBytes(hexString);

//To get ASCII value of the hex string.
string ASCIIresult = System.Text.Encoding.ASCII.GetString(dBytes);
MessageBox.Show(ASCIIresult, "Showing value in ASCII");

//To get the UTF8 value of the hex string
string utf8result = System.Text.Encoding.UTF8.GetString(dBytes);
MessageBox.Show(utf8result, "Showing value in UTF8");
解决方案

Since you are naming a variable hexString, I assume that the value is already encoded into a hex format.

This means the following will not work:

string hexString = "68656c6c6f2c206d79206e616d6520697320796f752e";
System.Text.UTF8Encoding  encoding=new System.Text.UTF8Encoding();
byte[] dBytes = encoding.GetBytes(hexString);

This is because you are treating the already encoded string as plain UTF8 text.

You are probably missing a step to convert the hex encoded string into a byte array.

You can do this using this SO post which shows this function:

public static byte[] StringToByteArray(String hex)
{
  int NumberChars = hex.Length/2;
  byte[] bytes = new byte[NumberChars];
  using (var sr = new StringReader(hex))
  {
    for (int i = 0; i < NumberChars; i++)
      bytes[i] = 
        Convert.ToByte(new string(new char[2]{(char)sr.Read(), (char)sr.Read()}), 16);
  }
  return bytes;
}

So, the end result would be this:

byte[] dBytes = StringToByteArray(hexString);

//To get ASCII value of the hex string.
string ASCIIresult = System.Text.Encoding.ASCII.GetString(dBytes);
MessageBox.Show(ASCIIresult, "Showing value in ASCII");

//To get the UTF8 value of the hex string
string utf8result = System.Text.Encoding.UTF8.GetString(dBytes);
MessageBox.Show(utf8result, "Showing value in UTF8");

这篇关于C#中转换成十六进制值,UTF8和ASCII的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 05:48