Click here to Skip to main content
11,487,406 members (69,679 online)
Rate this: bad
good
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}
            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: bad
good
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...
  Permalink  
Comments
glennPattonWork at 28-Jan-13 8:36am
   
Thanks, need to get my thinking right for this stand by for more dumb bottom questions!
OriginalGriff at 28-Jan-13 9:27am
   
:laugh:
You're welcome!
glennPattonWork at 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: bad
good
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);
         Console.WriteLine(formatString);
      }   
   }
}
// The example displays the following output: 
//       Decimal         Hex 
//     
//        -32768:       8000 
//           -27:       FFE5 
//             0:          0 
//          1042:        412 
//         32767:       7FFF

Cheers,
Edo
  Permalink  
Comments
glennPattonWork at 28-Jan-13 8:40am
   
Thanks for that!
Edo Tzumer at 28-Jan-13 8:42am
   
Sure thing! post a reply if you need some more help
glennPattonWork at 28-Jan-13 8:45am
   
No I think I have got it using Griffs example, but you never know!
Rate this: bad
good
Please Sign up or sign in to vote.

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
  Permalink  
Comments
glennPattonWork at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,063
1 OriginalGriff 8,287
2 Sascha Lefèvre 3,334
3 Maciej Los 2,946
4 Richard Deeming 2,370


Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 28 Jan 2013
Copyright © CodeProject, 1999-2015
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