# Convert integer to hexadecimal and back again

## Problem

What is the best way to convert the following?

B76 to 2934 (integer) (hex)

Let me clarify what I’m attempting to accomplish. In my database, I have User IDs that are saved as integers. Instead of requiring users to reference their IDs, I’d like to allow them to use the hex value. The primary reason is that it is shorter.

So I need to convert not only from integer to hex, but also from hex to integer.

Is there a simple method to accomplish this in C#?

## Solution #1

``````// Store integer 182
int intValue = 182;
// Convert integer 182 as a hex in a string variable
string hexValue = intValue.ToString("X");
// Convert the hex string back to the number
int intAgain = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);
``````

from http://www.geekpedia.com/KB8_How-do-I-convert-from-decimal-to-hex-and-hex-to-decimal.html

To retrieve exactly 4 digits with a leading 0, use.ToString(“X4”). For lowercase hex numerals, use ToString(“x4”) (likewise for more digits).

## Solution #2

Use:

``````int myInt = 2934;
string myHex = myInt.ToString("X");  // Gives you hexadecimal
int myNewInt = Convert.ToInt32(myHex, 16);  // Back to int again.
``````

For more information and examples, see How to: Convert Between Hexadecimal Strings and Numeric Types (C# Programming Guide).

## Solution #3

To convert it to hex, use the formula below.

``````public static string ToHex(this int value) {
return String.Format("0x{0:X}", value);
}
``````

And back again

``````public static int FromHex(string value) {
if ( value.StartsWith("0x", StringComparison.OrdinalIgnoreCase)) {
value = value.Substring(2);
}
return Int32.Parse(value, NumberStyles.HexNumber);
}
``````

## Solution #4

``````int valInt = 12;
Console.WriteLine(valInt.ToString("X"));  // C  ~ possibly single-digit output
Console.WriteLine(valInt.ToString("X2")); // 0C ~ always double-digit output
``````

## Solution #5

``````string HexFromID(int ID)
{
return ID.ToString("X");
}

int IDFromHex(string HexID)
{
return int.Parse(HexID, System.Globalization.NumberStyles.HexNumber);
}
``````

However, I seriously doubt the use of this. Your stated purpose is to reduce the value, which will happen, but that isn’t a goal in and of itself. You imply that you want to make it easy to recall or type.

You’ve taken a step backwards if you mean easier to remember. We know it’s the same size as before, but it’s encoded differently. Your users, on the other hand, will be unaware that the letters are limited to ‘A-F,’ and therefore the ID will occupy the same mental space for them as if any letter from ‘A-Z’ were permitted. So, rather than remembering a phone number, it’s more like remembering a GUID (of equivalent length).

If you’re talking about typing, instead of using the keypad, you’ll have to use the main section of the keyboard. Because it won’t be a word their fingers are familiar with, typing it will be more challenging.

Allowing them to choose a real username is a far better alternative.