Click here to Skip to main content
12,749,754 members (36,932 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
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}
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.
Posted 28-Jan-13 3:16am
Rate this: bad
Please Sign up or sign in to vote.

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
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!
Rate this: bad
Please Sign up or sign in to vote.

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);
// The example displays the following output: 
//       Decimal         Hex 
//        -32768:       8000 
//           -27:       FFE5 
//             0:          0 
//          1042:        412 
//         32767:       7FFF

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: bad
Please Sign up or sign in to vote.

Solution 3


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


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

glennPattonWork 28-Jan-13 8:46am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170215.1 | Last Updated 28 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100