Converting string value to hex decimal

前端 未结 5 2170
渐次进展
渐次进展 2020-12-16 06:59

i am making application in c#. In that implication i have string which contain decimal value as

string number=\"12000\"; 

The Hex equivale

相关标签:
5条回答
  • 2020-12-16 07:30

    If you want to convert it to hex string you can do it by

    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. If you put hex it'll be binary too. So there is no point.

    0 讨论(0)
  • 2020-12-16 07:32

    you can try something like this if its going to be int

    string number = "12000";
    int val = int.Parse(number);
    string hex = val.ToString("X");
    
    0 讨论(0)
  • 2020-12-16 07:35

    An int contains a number, not a representation of the number. 12000 is equivalent to 0x2ee0:

    int a = 12000;
    int b = 0x2ee0;
    a == b
    

    You can convert from the string "12000" to an int using int.Parse(). You can format an int as hex with int.ToString("X").

    0 讨论(0)
  • 2020-12-16 07:43

    Well you can use class String.Format to Convert a Number to Hex

    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);
    }
    
    0 讨论(0)
  • 2020-12-16 07:45
    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

    0 讨论(0)
提交回复
热议问题