This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

int to hex

 
Adithya Bhat
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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


Regards,
Rok
 
John de Michele
Rancher
Posts: 600
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rok:

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

John.
 
Adithya Bhat
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you John.

Adithya.
 
John de Michele
Rancher
Posts: 600
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adithya:

Glad I could help.

John.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic