This week's book giveaways are in the Refactoring and Agile forums.We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# int to hex

Greenhorn
Posts: 9
I have an integer

int x=6422616;

I need to convert this integer into hex and store it in y in 0x format

i.e int y=0x620058; (here 620058 is hex equivalent of 6422616).Can you please tell me how to do it???

John de Michele
Rancher
Posts: 600

Since you're storing it as an int, does it really matter which base you're writing it in?

John.

Rok Ć telcer
Ranch Hand
Posts: 101
Hi,

I guess this is what you're looking for:
- Integer.toHexString(int x);

Regards,
Rok

John de Michele
Rancher
Posts: 600
Rok:

Unfortunately, unless Adithya stores it in a String format, the issue is moot.

John.

Greenhorn
Posts: 9
Thank you guys for responding.
John i need it to be stored as hex.
And Rok this converts int to hex what about 0x prefix.
System.out.println(y);

i want its output to be 0x620058.

John de Michele
Rancher
Posts: 600

Then you'll need to store it as a String. Storing (base 10) 255 is no different than (base 16) 0xFF to Java. You can use Rok's suggestion and do something like this:

John.

Greenhorn
Posts: 9
But this doesn't store hex value in int,it stores in string.
Then how come this following code is valid???

John de Michele
Rancher
Posts: 600

As I was trying to explain to you, to Java there is no difference between these statements:

You can already store hex values as ints (and use Integer.toHexString() to display as a hex String). Java doesn't store any 'formatting' in primitives.

John.

Greenhorn
Posts: 9
Thank you John.