# Converting to Hexadecimal literals

May-Yoong Cheah

Greenhorn

Posts: 8

posted 13 years ago

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.

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

Posts: 3271

posted 13 years ago

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

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

Damien Howard

Ranch Hand

Posts: 456

posted 13 years ago

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.

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

Sheriff

Posts: 8898

5

posted 13 years ago

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.

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

Posts: 8

Mansi Dave

Ranch Hand

Posts: 49

Tom Purl

Ranch Hand

Posts: 104

posted 13 years ago

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 ]

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

Sheriff

Posts: 8898

5

posted 13 years ago

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

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

Spot false dilemmas now, ask me how!

(If you're not on the edge, you're taking up too much room.)