Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting string value to hex decimal

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

string number="12000"; 

The Hex equivalent of 12000 is 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.

like image 791
Dany Avatar asked Jan 05 '12 08:01

Dany


People also ask

How do you convert a string to a decimal?

let stringVal = Convert.ToString decimalVal printfn $"The decimal as a string is {stringVal}."

Can we convert string to hex in python?

To convert Python String to hex, use the inbuilt hex() method. The hex() is a built-in method that converts the integer to a corresponding hexadecimal string. For example, use the int(x, base) function with 16 to convert a string to an integer.

What is hex string format?

The format for coding a hexadecimal string mask is: X'yy...yy' The value yy represents any pair of hexadecimal digits that constitute a byte (8 bits). Each bit must be 1 (test bit) or 0 (ignore bit). You can specify up to 256 pairs of hexadecimal digits.


3 Answers

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

like image 160
John Woo Avatar answered Sep 21 '22 04:09

John Woo


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").

like image 20
Sjoerd Avatar answered Sep 23 '22 04:09

Sjoerd


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);
}
like image 28
Rosmarine Popcorn Avatar answered Sep 23 '22 04:09

Rosmarine Popcorn