1. Member
Join Date
Jul 2008
Posts
7
Rep Power
0

## [SOLVED] Base conversions

Howdy,

Not so much a question about code but logic.
I would like some advice on base conversions.
I would like to implement a utility that can convert from base8 to base16.

I'm not to au-fait with this and was wondering would it be easiest to convert to say base2 and then from base2 to base16 or should i try to convert directly from base8 to base16.

Thanks

2. Senior Member
Join Date
Nov 2008
Posts
286
Rep Power
9
If this isn't a programming exercise, look at Integer.parseInt() and Integer.toString(), both of which take a base parameter to convert from and to a given string representations of the number in a given base. (Even if it is a programming exercise, you might want to look at the source code and see how these methods are implemented, of course...)

If you're implementing it yourself, then yes you may want to think about optimising it for your specific conversion. You're going from base 8 to base 16:
- think about how many bits are in an octal (base 8) digit and how many bits in a hex (base 16) digit. How many digits in each base do you need before a whole number of base 8 digits has the same number of bits as a whole number of base 16 digits?
- when you've decided how many digits at a time you need to process from the source number, how do you extract those digits and convert them into an integer that the computer can do maths on?
- what's the formula/method you'll need to go from x base-8 digits to y base-16 digits? since your bases are both powers of 2, how can you do the division and multiplication quicker?

3. Member
Join Date
Jul 2008
Posts
7
Rep Power
0