将字符串值转换为hex十进制
我在c#中申请。 在这个含义中,我有一个包含十进制值的字符串
string number="12000";
hex等效值12000是0x2EE0。
在这里,我想将该hex值分配给整数变量as
int temp=0x2EE0.
请帮我转换那个号码。 提前致谢。
int包含数字,而不是数字的表示。 12000相当于0x2ee0:
int a = 12000; int b = 0x2ee0; a == b
您可以使用int.Parse()将字符串“12000”转换为int。 您可以使用int.ToString(“X”)将int格式化为hex。
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
那么你可以使用String.Format类将数字转换为hex
int value = Convert.ToInt32(number); string hexOutput = String.Format("{0:X}", value);
如果要将字符串关键字转换为hex,则可以执行此操作
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); }
如果要将其转换为hexstring
,则可以执行此操作
string hex = (int.Parse(number)).ToString("X");
如果您只想将数字设为hex。 这是不可能的。 Becasue计算机总是以二进制格式保存数字,所以当你执行int i = 1000
它在i
存储1000作为二进制。 如果你把hex它也将是二进制的。 所以没有意义。
如果它是int,你可以尝试这样的东西
string number = "12000"; int val = int.Parse(number); string hex = val.ToString("X");