• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reg. Integer literals

 
Sunita
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I would likt know how to convert numbers from hexadecimal format to decimal and Vice versa.
For ex:
int i=0xf;
then the value of i in decimal format is 15.(This is clear).

But
if int i=0xff or i=0xffe
then what's the value of i in decimal format?
Regards
Sunita
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
to convert an integer to Hex use Integer.toHexString(int i).
progamatically i am waiting for someone to tell how to convert form HexString to int.
if u want to do it manually search this site saying 0x as the keyword and u will find lots of pages one of which is
http://www.javaranch.com/ubb/Forum24/HTML/001272.html
http://www.javaranch.com/ubb/Forum24/HTML/000495.html
Rahul
[This message has been edited by rahul_mkar (edited June 08, 2000).]
 
Jakob Bosshard
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sunita
The hexadecimal representation is for the convenience of the
programmer for assignment and output. Internally ints are stored as bytes (binary). Two things on changing the representation:
int i = 0xFF;
System.out.println(i);
This will display the int as a decimal.
The wrapper class java.lang.Integer offers a few conversion methods (mostly static) that allow you to output the Integer in any of the representations returning a string.
int i = 10;
String s = new String(Integer.toHexString(i));
System.out.println(s);
Finally you can parse an int from a string giving the radix you want to use:
int j = Integer.parseInt("FF", 16);
System.out.println(j);
//output is 255
Just write some code with the java.lang.Integer class and find
out about all the representations.
Hope this will help
Regards
Jakob
 
Sunita
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya!!!Now Iam clear.
Thanks Guys.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic