将字符串值转换为十六进制十进制

Dan*_*any 3 c# string hex decimal

我在c#中申请.在这个含义中,我有一个包含十进制值的字符串

string number="12000"; 
Run Code Online (Sandbox Code Playgroud)

Hex等效值12000是0x2EE0.

在这里,我想将该十六进制值分配给整数变量as

int temp=0x2EE0.
Run Code Online (Sandbox Code Playgroud)

请帮我转换那个号码.提前致谢.

Joh*_*Woo 24

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
 */
Run Code Online (Sandbox Code Playgroud)

消息来源:http://msdn.microsoft.com/en-us/library/bb311038.aspx

  • 一个LINQ版本,当紧凑性更重要时:`String.Join("",input.Select(char => String.Format("{0:X}",Convert.ToInt32(char))))` (7认同)

Sjo*_*erd 8

int包含数字,而不是数字的表示.12000相当于0x2ee0:

int a = 12000;
int b = 0x2ee0;
a == b
Run Code Online (Sandbox Code Playgroud)

您可以使用int.Parse()将字符串"12000"转换为int .您可以使用int.ToString("X")将int格式化为十六进制.


Bur*_*imi 6

那么你可以使用String.Format类将数字转换为十六进制

int value = Convert.ToInt32(number);
string hexOutput = String.Format("{0:X}", value);
Run Code Online (Sandbox Code Playgroud)

如果要将字符串关键字转换为十六进制,则可以执行此操作

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);
}
Run Code Online (Sandbox Code Playgroud)