This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Converting to Hexadecimal literals 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 to Hexadecimal literals" Watch "Converting to Hexadecimal literals" New topic
Author

Converting to Hexadecimal literals

May-Yoong Cheah
Greenhorn

Joined: Apr 02, 2003
Posts: 8
I know that it is easy to convert a small number like 1 to hex which is 0x0001.
But how do you convert a big number like 2147483647 to hex? Is there a formula that I can follow? And I am wondering if in the exam they will test you on converting big numbers to hex.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by May Yoong Cheah:
I know that it is easy to convert a small number like 1 to hex which is 0x0001.
But how do you convert a big number like 2147483647 to hex? Is there a formula that I can follow? And I am wondering if in the exam they will test you on converting big numbers to hex.

You can do a search through this forum and find lots of threads that deal with this.
I find that the best method is to continutally divide the number by the base you want to convert to. Then, the remainders make up the value in the new base. Like this:

Then, your answer is the remainders, in reverse order:
2147483647 = 0x7FFFFFFF
You can do the same to convert to binary - just divide by 2 instead of 16.
Of course, I can't imagine them giving you a number this nasty to do on the exam - the math would take too long.
I hope that helps,
Corey


SCJP Tipline, etc.
Damien Howard
Ranch Hand

Joined: Apr 01, 2003
Posts: 456
I'm sure there is a better way, but what I usually do is convert to binary first, and then you group every four binary digits together and convert them to HEX.
For example:
2531 is 100111100011 in binary
so the first four digits from the left are 0011 which is a 3 in dec and hex
the next four are 1110 which is a 14 or an E in hex
the next four are 1001 which is a 9 in dec and hex
so 2531 in HEX is 0x3E9
If you don't have enough digits to group by fours, you just add some leading 0s until you do.
It can be a time consuming process and there might be a better way, but that is how I convert to HEX.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8801
    
    5
May -
It is good to know how to make these conversions, and the better news is you won't have to on the exam!
On the other hand, the exam will test your knowledge of bit shifting, so you might have to deal with an expression like x << 31.
So it IS important to know how many bits each of the primitive types has, and it IS important to understand 2's complement.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
May-Yoong Cheah
Greenhorn

Joined: Apr 02, 2003
Posts: 8
Thanks Corey, Damien and Bert for all your solutions...
Mansi Dave
Ranch Hand

Joined: Aug 06, 2002
Posts: 49
Bert,
To what extent will we need to know how to evaluate bit shifting expressions? Is there any shortcut(quick way) for this?
thanks.
Mansi
Tom Purl
Ranch Hand

Joined: May 24, 2002
Posts: 104
If you use the >> operator, then you divide the left operator by 2 to the second operator.
Ex.
12 >> 2
...is really
12 / 2^2
...which is really
12 / 4
...or 3
If you use the << operator, then you multiply the left operator by 2 times the second operator.
Ex.
12 << 2
...is really
12 * 2^2
...which is really
12 * 4
...or 48
Hope that helps!
[ April 07, 2003: Message edited by: Tom Purl ]

Tom Purl<br />SCJP 1.4
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8801
    
    5
Mansi -
You are very likely to encounter the following topics grouped together into a single question.
- The functionality of >>, or <<, or >>>
- The size of integral primitives in bits,
- How two's complement works.
For example:
short x = 1 << 15;
Is x a really small number?
Is x a really big positive number?
Is x a really big negative number?
See how all three of those topics are used together. But notice that you don't have to be a human calculator.
-Bert
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Converting to Hexadecimal literals
 
Similar Threads
Hexadecimal and decimal convert code
Hex and Octal
Long Hexadecimal numbers
long to Integer conversion
Ascii to Hex