File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes int to hex Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "int to hex" Watch "int to hex" New topic
Author

int to hex

Adithya Bhat
Greenhorn

Joined: Nov 06, 2009
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

Joined: Mar 09, 2009
Posts: 600
Adithya:

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

John.
Rok Ć telcer
Ranch Hand

Joined: Nov 03, 2009
Posts: 101
Hi,

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


Regards,
Rok


SCJP, SCWCD
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Rok:

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

John.
Adithya Bhat
Greenhorn

Joined: Nov 06, 2009
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

Joined: Mar 09, 2009
Posts: 600
Adithya:

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.
Adithya Bhat
Greenhorn

Joined: Nov 06, 2009
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

Joined: Mar 09, 2009
Posts: 600
Adithya:

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.
Adithya Bhat
Greenhorn

Joined: Nov 06, 2009
Posts: 9
Thank you John.

Adithya.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Adithya:

Glad I could help.

John.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: int to hex
 
Similar Threads
how to apply hex value to color class
how many digits are allowed in a HEX number
Generate next hexadecimal?
unicode into char
Convert byte to its hex format?