我需要将字符转换为十六进制值。请参考Ascii表格,但下面列出了一些示例:
- char 1 = 31 2 = 32 3 = 33 4 = 34 5 = 35 A = 41 a = 61 等等
因此,字符串str =“12345”; 需要获得转换后的str =“3132333435”。
我需要将字符转换为十六进制值。请参考Ascii表格,但下面列出了一些示例:
因此,字符串str =“12345”; 需要获得转换后的str =“3132333435”。
我想这就是你所需的全部:
string finalValue;
byte[] ascii = Encoding.ASCII.GetBytes(yourString);
foreach (Byte b in ascii)
{
finalValue += b.ToString("X");
}
更多信息请参见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
string.Join("", from c in "12345" select ((int)c).ToString("X"));
string s = "abc123";
foreach(char c in s)
{
Response.Write((int)c + ",");
}
var result = "12345".Aggregate("", (res, c) => res + ((byte)c).ToString("X"));
这将返回"3132333435"
,就像您要求的一样 :)