# Hexadecimal and Octal to decimal conversions

Sridhar Katakam

Greenhorn

Posts: 8

posted 15 years ago

Hi all

Consider this question:

Given the following code fragment from a class definition, which of the following statements is true? [Check all correct answers]

1. int Aval = 0x65;

2. byte Bval = 065;

A) The variable Aval has been initialized with a hexadecimal format literal, and Bval has been initialized with an octal format literal.

B) Both Aval and Bval contain 65.

C) The logical test Aval > Bval would evaluate True.

D) The compiler would report a NumberFormatException on line 1.

Answer: Answers A and C are correct. The hexadecimal literal evaluates to 101 decimal, and the octal literal evaluates to 53 decimal. Answer B is incorrect because line 1 uses a hexadecimal literal and line 2 an octal literal. Answer d is spurious because it is the Java runtime that reports exceptions, not the compiler.

Can any one tell me how to convert hexadecimal and Octal literals to decimals ?

like Hexadecimal 0x65 is equivalent to Decimal 101

Octal 065 is equivalent to Decimal 53.

~Sri~

- 0

Hi all

Consider this question:

Given the following code fragment from a class definition, which of the following statements is true? [Check all correct answers]

1. int Aval = 0x65;

2. byte Bval = 065;

A) The variable Aval has been initialized with a hexadecimal format literal, and Bval has been initialized with an octal format literal.

B) Both Aval and Bval contain 65.

C) The logical test Aval > Bval would evaluate True.

D) The compiler would report a NumberFormatException on line 1.

Answer: Answers A and C are correct. The hexadecimal literal evaluates to 101 decimal, and the octal literal evaluates to 53 decimal. Answer B is incorrect because line 1 uses a hexadecimal literal and line 2 an octal literal. Answer d is spurious because it is the Java runtime that reports exceptions, not the compiler.

Can any one tell me how to convert hexadecimal and Octal literals to decimals ?

like Hexadecimal 0x65 is equivalent to Decimal 101

Octal 065 is equivalent to Decimal 53.

~Sri~

Lori Battey

Ranch Hand

Posts: 37

posted 15 years ago

- 0

I put this together to help some others...I could use some input on shifting of negative numbers but the remainder should be useful! I have it in a Word 97 document and am not sure how the formating will show up as I am copying/pasting it in here. If anyone would like the word document emailed, leave me you email address and I'll forward it.

***********************************

Numeric Conversions and Bit Shifting

To convert a decimal number into binary, octal or hexadecimal, you need to understand �powers� of each base system and be able to figure the positional value representation. If I would have to convert from octal to binary, or hexadecimal to binary, I would convert the original value to decimal and then convert to the second base system. There may be an easier way but this works for me!

Also, if you need to shift a negative number, get the positive value in binary and then reverse the bits. Do your shifting, and then reverse your bits again to find the value of the shifted number. Remember that your number could be positive or negative based on the type of shift you do � get the value first, then make the sign appropriate for the shift.

Binary positional valuesBASE 2 (x2 or to the power of 2)

5096204810245122561286432168421

xxxxx

binary #10110011

positional value128032160021

|||||

128||||

32-------------|||

16---------------------||

2---------------------------------------------|

1-----------------------------------------------------

total decimal value179

When doing bit shifting, the left operand is assumed to be an int (32 bits) unless it is specifically stated to be a long (64 bits). The shift will be performed upon the left operand value the number of times stated by the right hand operand. Something to remember is that shifting is only able to be performed by the number of bits available � 32 or 64. If you are given a shift larger than the bit value of the left operand you need to modulo by the bits available. In our instance above 179 (int) >> 45 is actually 179 >> 13 (45 % 32).

The effect of >> is dividing by 2 for every shift (remainders are truncated, not rounded) with the filled in bits being zeros for positive numbers and ones for negative numbers. Using the example above, 179 >>> 2 is the same as ((179 / 2 = 89) / 2 = 44). See the example below.

5096204810245122561286432168421

xxx

binary # right shifted 2101100

positional value3208400

|||

32||

8-------------|

4---------------------

total decimal value44

The effect of >>> is the same except the filled in bits are always 0, automatically making negative numbers positive.

The effect of << is multiplying by 2 for every shift. The filled in bits are always 0. Depending on the size of the shift, a positive number could be negative and a negative number could be positive.

Octal positional valuesBASE 8 (x8 or to the power of 8)

20971522621443276840965126481

xxxxx

Octal # starts with 017426

positional value40963584256166

|||||

4096---------------------------||||

3584-----------------------------------|||

256-------------------------------------------||

16---------------------------------------------------|

6-----------------------------------------------------------

total decimal value7958

Hexidecimal positional valuesBASE 16 (x16 or to the power of 16)

0 to 9 then A to F

1048576655364096256161

xxxxxA=10

Hex # starts with 0x196AEB=11

positional value6553636864153616014C=12

|||||D=13

65536---------------------------||||E=14

36864-----------------------------------|||F=15

1536-------------------------------------------||

160---------------------------------------------------|

14-----------------------------------------------------------

total decimal value104110

Hopefully this helps!

Lori

***********************************

Numeric Conversions and Bit Shifting

To convert a decimal number into binary, octal or hexadecimal, you need to understand �powers� of each base system and be able to figure the positional value representation. If I would have to convert from octal to binary, or hexadecimal to binary, I would convert the original value to decimal and then convert to the second base system. There may be an easier way but this works for me!

Also, if you need to shift a negative number, get the positive value in binary and then reverse the bits. Do your shifting, and then reverse your bits again to find the value of the shifted number. Remember that your number could be positive or negative based on the type of shift you do � get the value first, then make the sign appropriate for the shift.

Binary positional valuesBASE 2 (x2 or to the power of 2)

5096204810245122561286432168421

xxxxx

binary #10110011

positional value128032160021

|||||

128||||

32-------------|||

16---------------------||

2---------------------------------------------|

1-----------------------------------------------------

total decimal value179

When doing bit shifting, the left operand is assumed to be an int (32 bits) unless it is specifically stated to be a long (64 bits). The shift will be performed upon the left operand value the number of times stated by the right hand operand. Something to remember is that shifting is only able to be performed by the number of bits available � 32 or 64. If you are given a shift larger than the bit value of the left operand you need to modulo by the bits available. In our instance above 179 (int) >> 45 is actually 179 >> 13 (45 % 32).

The effect of >> is dividing by 2 for every shift (remainders are truncated, not rounded) with the filled in bits being zeros for positive numbers and ones for negative numbers. Using the example above, 179 >>> 2 is the same as ((179 / 2 = 89) / 2 = 44). See the example below.

5096204810245122561286432168421

xxx

binary # right shifted 2101100

positional value3208400

|||

32||

8-------------|

4---------------------

total decimal value44

The effect of >>> is the same except the filled in bits are always 0, automatically making negative numbers positive.

The effect of << is multiplying by 2 for every shift. The filled in bits are always 0. Depending on the size of the shift, a positive number could be negative and a negative number could be positive.

Octal positional valuesBASE 8 (x8 or to the power of 8)

20971522621443276840965126481

xxxxx

Octal # starts with 017426

positional value40963584256166

|||||

4096---------------------------||||

3584-----------------------------------|||

256-------------------------------------------||

16---------------------------------------------------|

6-----------------------------------------------------------

total decimal value7958

Hexidecimal positional valuesBASE 16 (x16 or to the power of 16)

0 to 9 then A to F

1048576655364096256161

xxxxxA=10

Hex # starts with 0x196AEB=11

positional value6553636864153616014C=12

|||||D=13

65536---------------------------||||E=14

36864-----------------------------------|||F=15

1536-------------------------------------------||

160---------------------------------------------------|

14-----------------------------------------------------------

total decimal value104110

Hopefully this helps!

Lori

Lori Battey<br />SCJP2

Pat Barrett

Ranch Hand

Posts: 63

posted 15 years ago

- 0

Here's a tidbit I learned about converting octal/hex directly into binary...

Let's start with an Octal value of 31 ( 25 decimal ). You would take each individual digit and convert it into a 3 digit binary representation. The '3' would become 011 and the '1' would become 001. You would concatenate them together to end up with the binary equivalent of octal 31;

011 001 ---> 11001 (you can toss the preceeding zero)

Hex works the same way, but you'd convert the separate digits into binary groups of 4. A Hex number such as 4F (79 decimal) would therefore be;

0100 1111 ----> 1001111

The reverse also works quite nicely. From right to left, break a binary number into groups of 3 (octal) or 4 (hex) and convert those groups into their octal/hex equivalent.

HTH

Pat B.

[This message has been edited by Pat Barrett (edited January 31, 2001).]

[This message has been edited by Pat Barrett (edited January 31, 2001).]

Let's start with an Octal value of 31 ( 25 decimal ). You would take each individual digit and convert it into a 3 digit binary representation. The '3' would become 011 and the '1' would become 001. You would concatenate them together to end up with the binary equivalent of octal 31;

011 001 ---> 11001 (you can toss the preceeding zero)

Hex works the same way, but you'd convert the separate digits into binary groups of 4. A Hex number such as 4F (79 decimal) would therefore be;

0100 1111 ----> 1001111

The reverse also works quite nicely. From right to left, break a binary number into groups of 3 (octal) or 4 (hex) and convert those groups into their octal/hex equivalent.

HTH

Pat B.

[This message has been edited by Pat Barrett (edited January 31, 2001).]

[This message has been edited by Pat Barrett (edited January 31, 2001).]

Don't get me started about those stupid light bulbs. |