12,749,754 members (36,932 online)
Rate this:
See more:
Hi All,

This is related to a question on Friday "Some String Builder Problems" which is sorted now a bit of grief with converting the string which contains integers to Hex values. I am trying after a little playing to get a value which is an integer converted to a string I am trying to do the following which has worked in the past
```//parse an Int to a Hex

Value_Hex =(Value.ToString("x"));// String.Format(Value{"0:x2",numericUpDown1.Value}
MessageBox.Show(Value_Hex.ToString());
```
but I can't seem to get it working today that is the right way of doing it?
A method I have used to convert from base 10 to Hex is
```Value_Int = int.Parse(ValueUPDwn.ToString(), System.Globalization.NumberStyles.HexNumber);
```
but I can't see via the intellisense that would be of use.
Glenn
Posted 28-Jan-13 3:16am

Rate this:

## Solution 1

To convert, I use these methods:
Hex string to int:
`int i = Convert.ToInt32("18da06f1",16);`

Int to hex string:
`string s = i.ToString("X");`
These work every time for me...
glennPattonWork 28-Jan-13 8:36am

Thanks, need to get my thinking right for this stand by for more dumb bottom questions!
OriginalGriff 28-Jan-13 9:27am

:laugh:
You're welcome!
glennPattonWork 28-Jan-13 10:12am

Here comes the first "dumb bottom question" but I think it warrents a question on its own!
Glenn
Rate this:

## Solution 2

Hi Glenn,

Check out the following example:

```public class Example
{
public static void Main()
{
short[] values= { Int16.MinValue, -27, 0, 1042, Int16.MaxValue };
Console.WriteLine("{0,10}  {1,10}\n", "Decimal", "Hex");
foreach (short value in values)
{
string formatString = String.Format("{0,10:G}: {0,10:X}", value);
Console.WriteLine(formatString);
}
}
}
// The example displays the following output:
//       Decimal         Hex
//
//        -32768:       8000
//           -27:       FFE5
//             0:          0
//          1042:        412
//         32767:       7FFF
```

Cheers,
Edo
glennPattonWork 28-Jan-13 8:40am

Thanks for that!
Edo Tzumer 28-Jan-13 8:42am

Sure thing! post a reply if you need some more help
glennPattonWork 28-Jan-13 8:45am

No I think I have got it using Griffs example, but you never know!
Rate this:

## Solution 3

Hi,

There are different solutions:

```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);```

or

`string hexOutput = String.Format("{0:X}", value);`

Thanks
glennPattonWork 28-Jan-13 8:46am

Thanks!

Top Experts
Last 24hrsThis month
 Karthik Bangalore 325 Graeme_Grant 249 F-ES Sitecore 215 OriginalGriff 175 Afzaal Ahmad Zeeshan 125
 OriginalGriff 3,833 Peter Leow 2,871 Karthik Bangalore 2,065 ppolymorphe 2,052 Richard MacCutchan 1,409