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.

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

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.

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

posted

0

Thanks Corey, Damien and Bert for all your solutions...

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: 8829

5

posted

0

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