[英]Converting string value to hex decimal
i am making application in c#. 我在C#中进行应用。 In that implication i have string which contain decimal value as
在这种暗示我有包含十进制值作为
string number="12000";
The Hex equivalent of 12000 is 0x2EE0. 相当于12000的十六进制是0x2EE0。
Here i want to assign that hex value to integer variable as 在这里我想将十六进制值分配给整数变量作为
int temp=0x2EE0.
Please help me to convert that number. 请帮我转换这个数字。 Thanks in advance.
提前致谢。
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
*/
SOURCE: http://msdn.microsoft.com/en-us/library/bb311038.aspx 消息来源: http : //msdn.microsoft.com/en-us/library/bb311038.aspx
An int contains a number, not a representation of the number. 一个int包含一个数字,而不是数字的表示形式。 12000 is equivalent to 0x2ee0:
12000等效于0x2ee0:
int a = 12000;
int b = 0x2ee0;
a == b
You can convert from the string "12000" to an int using int.Parse() . 您可以使用int.Parse()将字符串“ 12000”转换为int。 You can format an int as hex with int.ToString("X") .
您可以使用int.ToString(“ X”)将int格式化为十六进制。
Well you can use class String.Format to Convert a Number to Hex 好吧,您可以使用String.Format类将数字转换为十六进制
int value = Convert.ToInt32(number);
string hexOutput = String.Format("{0:X}", value);
If you want to Convert a String Keyword to Hex you can do it 如果要将字符串关键字转换为十六进制,则可以执行此操作
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);
}
If you want to convert it to hex string
you can do it by 如果您想将其转换为十六进制
string
,可以通过
string hex = (int.Parse(number)).ToString("X");
If you want to put only the number as hex. 如果只想将数字作为十六进制。 Its not possible.
这是不可能的。 Becasue computer always keeps number in binary format so When you execute
int i = 1000
it stores 1000 as binary in i
. 因为计算机总是以二进制格式保存数字,所以当您执行
int i = 1000
它将1000作为二进制存储在i
。 If you put hex it'll be binary too. 如果您使用十六进制,它将也是二进制的。 So there is no point.
所以没有意义。
you can try something like this if its going to be int 您可以尝试这样的事情,如果它是int
string number = "12000";
int val = int.Parse(number);
string hex = val.ToString("X");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.