在c#中将ascii代码转换为十六进制字符串
问题描述:
?
如何将!q44499:²?о888?=º88Š转换为19217134343439393AB205D0BE383838133DBA38388A。我试过Encoding.Default.GetBytes(!q44499:²?о888?=º88Š)。但对于字符²,我得到3f,而不是b2。请帮助。
Hi,
How to convert "!q44499:²Ð¾888=º88Š" to "19217134343439393AB205D0BE383838133DBA38388A". I have tried Encoding.Default.GetBytes("!q44499:²Ð¾888=º88Š"). But for character "²" I get "3f", instead of "b2".Please help.
答
也许这个答案可行???
Maybe this answer would work???
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
class Convert
{
public void DoConversion()
{
string str = "test";
byte[] byteArray = null;
System.Text.StringBuilder hexNumbers = new System.Text.StringBuilder();
byteArray = System.Text.ASCIIEncoding.ASCII.GetBytes(str);
for (int i = 0; i <= byteArray.Length - 1; i++) {
hexNumbers.Append(byteArray[i].ToString("x"));
}
Interaction.MsgBox(hexNumbers.ToString());
// Out put will be 74657374
// The following code will reverse the operation and verify the output:
string st = hexNumbers.ToString();
string com = "";
for (x = 0; x <= st.Length - 1; x += 2) {
com += Strings.ChrW(Convert.ToInt32("&H" + st.Substring(x, 2)));
}
Interaction.MsgBox(com);
}
}
http://stackoverflow.com/questions/25129402/how-to-convert-ascii-to-hexdecimal -in-vb-net [ ^ ]
public static string AsciiToHex(string asciiString)
{
StringBuilder builder = new StringBuilder();
foreach (char c in asciiString)
{
builder.Append(Convert.ToInt32(c).ToString("X"));
}
return builder.ToString();
}