Click here to Skip to main content
15,906,329 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
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
C#
//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
C#
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

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

Int to hex string:
C#
string s = i.ToString("X");
These work every time for me...
 
Share this answer
 
Comments
glennPattonWork3 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!
glennPattonWork3 28-Jan-13 10:12am    
Here comes the first "dumb bottom question" but I think it warrents a question on its own!
Glenn
Hi Glenn,

Check out the following example:

C#
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
 
Share this answer
 
Comments
glennPattonWork3 28-Jan-13 8:40am    
Thanks for that!
Joezer BH 28-Jan-13 8:42am    
Sure thing! post a reply if you need some more help
glennPattonWork3 28-Jan-13 8:45am    
No I think I have got it using Griffs example, but you never know!
Hi,

There are different solutions:

C#
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
 
Share this answer
 
Comments
glennPattonWork3 28-Jan-13 8:46am    
Thanks!

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900