6

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.

Dany
  • 2,034
  • 8
  • 34
  • 54
  • int temp = int.Parse(number); The int variable will have the same value, whether you assign it a decimal number or a hexadecimal number. What you perhaps want instead is to get the hex string, such as: string hexStr = int.Parse(number).ToString("X"); – Julian Jan 05 '12 at 08:33
  • check out http://www.geekpedia.com/KB8_How-do-I-convert-from-decimal-to-hex-and-hex-to-decimal.html – shakib Jan 05 '12 at 08:34
  • If you assign hex to an integer value it will always contain binary. – Shiplu Mokaddim Jan 05 '12 at 08:34
  • Hasn't that question been answered multiple times on stackoverflow? – juergen d Jan 05 '12 at 08:34
  • If you Change the Places of temp with number this will work like a charm . – Rosmarine Popcorn Jan 05 '12 at 08:41

5 Answers5

25
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

John Woo
  • 258,903
  • 69
  • 498
  • 492
  • 7
    A LINQ version, for when compactness matters more: `String.Join(" ", input.Select(char => String.Format("{0:X}", Convert.ToInt32(char))))` – Arithmomaniac Jul 27 '12 at 18:22
8

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

Sjoerd
  • 74,049
  • 16
  • 131
  • 175
6

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);
}
Rosmarine Popcorn
  • 10,761
  • 11
  • 59
  • 89
3

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.

Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
1

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");
V4Vendetta
  • 37,194
  • 9
  • 78
  • 82