tags:

views:

17623

answers:

8

How do you convert between Hex numbers and decimal numbers in C#?

A: 
String stringrep = myintvar.ToString("X");

int num = int.Parse("FF", System.Globalization.NumberStyles.HexNumber);
Sklivvz
+2  A: 

looks like you can say:

Convert.ToInt64(value, 16)

To get the Decimal from Hex, and the other way around:

otherVar.ToString("X");
Jesper Blad Jensen aka. Deldy
A: 

From geekpedia :

// Store integer 182
int decValue = 182;
// Convert integer 182 as a hex in a string variable
string hexValue = decValue.ToString("X");
// Convert the hex string back to the number
int decAgain = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);
Rob
+5  A: 

Hex -> decimal:

Convert.ToInt64(hexValue, 16);

Decimal -> Hex

string.format("{0:x}", decValue);
Jonathan
A: 

Here is a link to the MSDN reference with the Formatting Numbers: http://msdn.microsoft.com/en-us/library/s8s7t687(VS.80).aspx

MagicKat
+23  A: 

To convert from Decimal to Hex do...

string hexValue = decValue.ToString("X");

To convert from Hex to Decimal do either...

int decValue = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);

or

int decValue = Convert.ToInt32(hexValue, 16);
Andy McCluggage
A: 

Wow! In the time it took me to find out elsewhere and type my answer in I got 6 other answers. I hope looking elsewhere continues to be that pointless. This site is awesome!

Now, whose answer is the best...?

Andy McCluggage
I would pick the quickest correct answer.
vitule
A: 
    static string chex(byte e)                  // Convert a byte to a string representing that byte in hexadecimal
    {
        string r = "";
        string chars = "0123456789ABCDEF";
        r += chars[e >> 4];
        return r += chars[e &= 0x0F];
    }           // Easy enough...

    static byte CRAZY_BYTE(string t, int i)     // Take a byte, if zero return zero, else throw exception (i=0 means false, i>0 means true)
    {
        if (i == 0) return 0;
        throw new Exception(t);
    }

    static byte hbyte(string e)                 // Take 2 characters: these are hex chars, convert it to a byte
    {                                           // WARNING: This code will make small children cry. Rated R.
        e = e.ToUpper(); // 
        string msg = "INVALID CHARS";           // The message that will be thrown if the hex str is invalid

        byte[] t = new byte[]                   // Gets the 2 characters and puts them in seperate entries in a byte array.
        {                                       // This will throw an exception if (e.Length != 2).
            (byte)e[CRAZY_BYTE("INVALID LENGTH", e.Length ^ 0x02)], 
            (byte)e[0x01] 
        };

        for (byte i = 0x00; i < 0x02; i++)      // Convert those [ascii] characters to [hexadecimal] characters. Error out if either character is invalid.
        {
            t[i] -= (byte)((t[i] >= 0x30) ? 0x30 : CRAZY_BYTE(msg, 0x01));                                  // Check for 0-9
            t[i] -= (byte)((!(t[i] < 0x0A)) ? (t[i] >= 0x11 ? 0x07 : CRAZY_BYTE(msg, 0x01)) : 0x00);        // Check for A-F
        }           

        return t[0x01] |= t[0x00] <<= 0x04;     // The moment of truth.
    }
Ecstatic Coder