假设我有这个字符串
string str = "1234"
我需要一个将这个字符串转换成这个字符串的函数:
"0x31 0x32 0x33 0x34"
网上查了很多类似的东西,都没有这个问题的答案。
最佳答案
string str = "1234";
char[] charValues = str.ToCharArray();
string hexOutput="";
foreach (char _eachChar in charValues )
{
// Get the integral value of the character.
int value = Convert.ToInt32(_eachChar);
// Convert the decimal value to a hexadecimal value in string form.
hexOutput += String.Format("{0:X}", value);
// to make output as your eg
// hexOutput +=" "+ String.Format("{0:X}", value);
}
//here is the HEX hexOutput
//use hexOutput
关于c# - 从字符串 ascii 转换为字符串 Hex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15920741/