• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Base conversion

 
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the SCJP exam in 2 weeks and would like to know a simple way to remember number base conversions.

i.e:

System.out.println(0107);

How would one, before viewing the result, calculate this the simplest way posible?
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First number not 0, unless the zero has a decimal point on its right side, is base 10
0 as first is Octal
0x is Hex.

Mark
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To convert any base number with n digits to decimal do the following:

1) Number the digits right to left from 0 to n-1.

In your case, 0107 is actually 107 and the leading 0 tells us its in octal.

so we have

107 <--- number
210 <--- digit numbers

2) now calculate the following sum. For each digit, multiply the digit by the number base to the power of the digit number, and add to the total.

In the case of 107 octal we have

1*8^2 + 0*8^1 + 7*8^0 = 64 + 0 + 7 = 71 (decimal)


To convert from binary to octal take the binary number and group the digits in threes starting from right to left. Convert each three binary digits to an octal digit and you have the number.

To convert from binary to hex take the binary number and group the digits in fours starting from right to left. Convert each four binary digits to a hex digit and you have the number.

So if you have the number 11110010100101

To octal we have

11 110 010 100 101
3 6 2 4 5

so we get 36245 octal

To hex we have

11 1100 1010 0101
3 c a 5

so we get 3ca5
 
Marcelo Ortega
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you both for your help, but i was looking for Sergei's explanation.
Thank you very much.

Cheers.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic