[英]Converting string value to hex decimal
我在C#中进行应用。 在这种暗示我有包含十进制值作为
string number="12000";
相当于12000的十六进制是0x2EE0。
在这里我想将十六进制值分配给整数变量作为
int temp=0x2EE0.
请帮我转换这个数字。 提前致谢。
string input = "Hello World!";
char[] values = input.ToCharArray();
foreach (char letter in values)
{
// Get the integral value of the character.
int value = Convert.ToInt32(letter);
// Convert the decimal value to a hexadecimal value in string form.
string hexOutput = String.Format("{0:X}", value);
Console.WriteLine("Hexadecimal value of {0} is {1}", letter, hexOutput);
}
/* Output:
Hexadecimal value of H is 48
Hexadecimal value of e is 65
Hexadecimal value of l is 6C
Hexadecimal value of l is 6C
Hexadecimal value of o is 6F
Hexadecimal value of is 20
Hexadecimal value of W is 57
Hexadecimal value of o is 6F
Hexadecimal value of r is 72
Hexadecimal value of l is 6C
Hexadecimal value of d is 64
Hexadecimal value of ! is 21
*/
消息来源: http : //msdn.microsoft.com/en-us/library/bb311038.aspx
一个int包含一个数字,而不是数字的表示形式。 12000等效于0x2ee0:
int a = 12000;
int b = 0x2ee0;
a == b
您可以使用int.Parse()将字符串“ 12000”转换为int。 您可以使用int.ToString(“ X”)将int格式化为十六进制。
好吧,您可以使用String.Format类将数字转换为十六进制
int value = Convert.ToInt32(number);
string hexOutput = String.Format("{0:X}", value);
如果要将字符串关键字转换为十六进制,则可以执行此操作
string input = "Hello World!";
char[] values = input.ToCharArray();
foreach (char letter in values)
{
// Get the integral value of the character.
int value = Convert.ToInt32(letter);
// Convert the decimal value to a hexadecimal value in string form.
string hexOutput = String.Format("{0:X}", value);
Console.WriteLine("Hexadecimal value of {0} is {1}", letter, hexOutput);
}
如果您想将其转换为十六进制string
,可以通过
string hex = (int.Parse(number)).ToString("X");
如果只想将数字作为十六进制。 这是不可能的。 因为计算机总是以二进制格式保存数字,所以当您执行int i = 1000
它将1000作为二进制存储在i
。 如果您使用十六进制,它将也是二进制的。 所以没有意义。
您可以尝试这样的事情,如果它是int
string number = "12000";
int val = int.Parse(number);
string hex = val.ToString("X");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.