Int to Hex

N

Nick

Consider this

//I want myVar to equal 0x41
int myVar;

int otherVar = GetItsValue; //returns a decimal 65, 65 is 41 in hex

//now i want to do something like this, obviously doesnt work
myVar = otherVar.ToHex(); //which would ideally be myVar = 0x41

//and then i want to pass myVar to a method as hex
MyMethod(someParam,myVar); // function only accepts hex, its an API call

In other words how do I make that otherVar into a hex?

Thanks.
Nick Z.
 
S

Shakir Hussain

Try this

string hexstring = otherVar.ToString("X");

To convert back again

int otherVar= int.Parse(hexstring ,
System.Globalization.NumberStyles.HexNumber);
 
S

Sijin Joseph

How about

otherVar =
int.Parse(otherVar.ToString(X),System.Globalization.NumberStyles.HexNumber);

:) :)
 
J

Jon Skeet [C# MVP]

Nick said:
Consider this

//I want myVar to equal 0x41
int myVar;

int otherVar = GetItsValue; //returns a decimal 65, 65 is 41 in hex

//now i want to do something like this, obviously doesnt work
myVar = otherVar.ToHex(); //which would ideally be myVar = 0x41

//and then i want to pass myVar to a method as hex
MyMethod(someParam,myVar); // function only accepts hex, its an API call

In other words how do I make that otherVar into a hex?

Other posters have shown you how to convert from an integer to a string
representation. However, I think you're missing something important
here - an int variable just contains a number. It doesn't have any base
associated with it - it's just a number. Saying:

int myVar = 0x41;

is *exactly* the same as

int myVar = 65;

If something claims it requires a hex representation, then chances are
it's a string parameter.
 
M

Mike Kitchen

Hi Nick
There are two methods you could use:

int nNumber = 1234;
nNumber.ToString("x");


or

string.Format( "{0:X}", 24 );


(X or x will output hex string). For a more flexible formating you should
use the Convert.ToString(number, base) method.

Some more information plus a snippet is included on my snippets page.


Hope this helps

Publicjoe
C# Tutorial at http://www.publicjoe.f9.co.uk/csharp/tut.html
C# Snippets at http://www.publicjoe.f9.co.uk/csharp/snip/snippets.html
C# Ebook at http://www.publicjoe.f9.co.uk/csharp/samples/ebook.html
VB Ebook at http://www.publicjoe.f9.co.uk/vbnet/samples/ebook.html
 
M

Mike Kitchen

int myVar = 0x41;

is *exactly* the same as

int myVar = 65;

If something claims it requires a hex representation, then chances are
it's a string parameter.

Very true Jon, Sometimes it is good to remind people that data can be
represented in different ways.
When I posted my reply, I instantly thought about output. It helps to read
the original post again.
If Nick just passes the integer, he is passing a hex value.

"Sometimes we do not see the wood from the trees because the forest is
burning".

Publicjoe
C# Tutorial at http://www.publicjoe.f9.co.uk/csharp/tut.html
C# Snippets at http://www.publicjoe.f9.co.uk/csharp/snip/snippets.html
C# Ebook at http://www.publicjoe.f9.co.uk/csharp/samples/ebook.html
VB Ebook at http://www.publicjoe.f9.co.uk/vbnet/samples/ebook.html
 
N

Nick

Mike said:
Very true Jon, Sometimes it is good to remind people that data can be
represented in different ways.
When I posted my reply, I instantly thought about output. It helps to read
the original post again.
If Nick just passes the integer, he is passing a hex value.

"Sometimes we do not see the wood from the trees because the forest is
burning".

Publicjoe
C# Tutorial at http://www.publicjoe.f9.co.uk/csharp/tut.html
C# Snippets at http://www.publicjoe.f9.co.uk/csharp/snip/snippets.html
C# Ebook at http://www.publicjoe.f9.co.uk/csharp/samples/ebook.html
VB Ebook at http://www.publicjoe.f9.co.uk/vbnet/samples/ebook.html

Thanks for pointing this out. I didnt realise that the data inside the int is the same. It turns out the API function accepts an
int in dec format as long as its equal to the hex representtaion.

Thank you.
Nick Z.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top