*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes converting hexa decimal  into int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "converting hexa decimal  into int " Watch "converting hexa decimal  into int " New topic
Author

converting hexa decimal into int

sanla palati
Ranch Hand

Joined: Oct 17, 2007
Posts: 36
Hi ,


How to convert the hexadecimal value 0xbeef into int.
Sekhar Kadiyala
Ranch Hand

Joined: Feb 17, 2004
Posts: 170
Are you looking for manual integer conversion or methods of wrapper classes?


PMP CSQA SCJP SCWCD SCBCD INS 21 INS 23
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Integer.decode() should do the trick, though I believe this is outside the scope of the SCJP exam.


SCJP 5.0
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy!

0xbeef is already an int, an int literal to be precise. Only it is not written in decimals but in hexadecimals.


int i = 0xbeef;
System.out.println(i);

prints out the value in decimals.


Yours,
Bu.


all events occur in real time
sanla palati
Ranch Hand

Joined: Oct 17, 2007
Posts: 36
Thanks for your replys guys.But Burkhard, how can i know that 0xbeef is an interger. I am confused.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Hexadecimal is not a separate data type. It is just a way to write numbers - in the base-16 numeric system instead of base-10 (decimal). When an integer literal in Java code starts with "0x", then it's in hexadecimal.

In addition to hexadecimal, you can write numbers in octal (the base-8 number system). A number is interpreted as octal if it starts with "0".


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Sanla asked

how can i know that 0xbeef is an interger


You're right, at a first glance 0xbeef looks more like something to eat than like an integer for many people.
Similarly, 0xCAFE looks more like something to drink than like the number 51966.

But you have to know about the other two ways of writing an integer.
Perhaps try this as a primer:



I'm terribly sorry that this output ends in a thirteen.
But hopefully you are not supersticious.
Ah, no, it doesn't end in a thirteen but in a nineteen!!!
Phew!

Yours,
Bu.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: converting hexa decimal into int
 
Similar Threads
can anyone please check my method
integer literals
getting values from JTable and putting them into textfields
Generating thumbnail on unix
What is the difference between these statements