需要将string/char转换为ascii值

SA.*_*SA. 6 c# asp.net visual-studio winforms

我需要将char转换为十六进制值.请参阅Ascii表,但我有几个例子如下:

  • char 1 = 31 2 = 32 3 = 33 4 = 34 5 = 35 A = 41 a = 61等

因此string str ="12345"; 需要获得转换后的str ="3132333435"

Jim*_*ert 8

我认为这就是你所需要的:

string finalValue;
byte[] ascii = Encoding.ASCII.GetBytes(yourString);
foreach (Byte b in ascii) 
{
  finalValue += b.ToString("X");
}
Run Code Online (Sandbox Code Playgroud)

有关MSDN的更多信息:http://msdn.microsoft.com/en-us/library/system.text.encoding.ascii.aspx

编辑:至十六进制:

string finalValue;
int value;
foreach (char c in myString)
{
  value = Convert.ToInt32(c);
  finalValue += value.ToString("X"); 
  // or finalValue = String.Format("{0}{1:X}", finalValue, value);
}
// use finalValue
Run Code Online (Sandbox Code Playgroud)