• ### What is your GameDev Story?

#### Archived

This topic is now archived and is closed to further replies.

# Counting Unit Conversion

This topic is 6117 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Does anyone here know how to convert from a base 10 counting system to a base 8? And vice versa? I have no clue as to where to start with this, and yes it does apply to a game. The game i am working on has 2 distinct languges, and the people using them use a base 8 counting system

##### Share on other sites
If you''re working in C or C++, conversion is not really hard, because you have bitwise access. If your number is represented binarily as

10011001101

You can group digits into packets of 3, and convert each group on its own

010 011 001 101
2 3 1 5 = 2315

ie.: 011 = 0 * 2 ^ 2 + 1 * 2 ^ 1 + 1 * 2 ^ 0 = 3

Look into the bitwise operators << >> | and & for bit manipulation.

There are other ways to convert from one base to another. You could try using logs. It''s a good exercise.

Cédric

##### Share on other sites
Small example :

decimal to octal :
1457 = 182 * 8 + 1             182 =  22 * 8 + 6             22 =   2 * 8 + 6             2 =   0 * 8 + 2
Thus 1457dec = 2661oct

octal to decimal :
2661oct = 2*8^3  + 6*8^2  + 6*8^1  + 1*8^0  = 1457dec
As Cedricl said, you can easily convert binary to octal and vice versa.

Hope it helps...

[edited by - Bloodscourge on April 19, 2002 6:33:56 PM]

##### Share on other sites
ummmm... i was thinking more along the lines of :
1 2 3 4 5 6 7 10

What ?

##### Share on other sites
quote:
Original post by black_mage_s
ummmm... i was thinking more along the lines of :
1 2 3 4 5 6 7 10

And that''s what you got...

##### Share on other sites
Ahhhhh, OK!

You mean :
 1 2 3  4 5 6 7101112131415161720 . . .

Sorry for the laaaaaaaaaaate understanding!

[edited by - Bloodscourge on April 19, 2002 8:34:20 PM]

##### Share on other sites
You can use modulus divides to convert. Say you wanted to convert 101 to base 7. 101%7 = 3, 101/7 = 14, 14%7 = 0, 14/7 = 2, 2%7 = 2, so 101 in base 10 equals 203 in base 7. You can work the opposite way by using ln(n)/ln(base)+1 to find how many digits there are to n in the given base. You can also do decimals by multiplying by the base and taking the integer portion. So to convert .101 to base 7 would be .101*7=.707, .707*7=4.949, .949*7=6.643, .643*7=4.501, .501*3.507, .507*7=3.549, .549*7=3.843, .843*7=5.901, .901*7=6.307, .307*7=2.149, .149*7=1.043, .043*7=.301, .301*7=2.107, .107*7=.749, .749*7=5.243, .243*7=1.701, .701*7=4.907, .907*7=6.349, .349*7=2.443, .443*7=3.101 and finally a repeat. So .04643335621020514623 repeating infinitely. Perhaps not a good choice in examples for decimals.

##### Share on other sites
#include <stdlib.h>#include <stdio.h>int main(int argc, char **argv){    long i, n;    char str[64];    for (i = 1; i < argc; i++) {        n = strtol(argv[ i ], NULL, 10);        ltoa(n, str, 8);        printf("%s base 10 = %s base 8\n", argv[ i ], str);    }    return 0;}

[edited by - Jeff K on April 19, 2002 11:35:27 PM]

##### Share on other sites
hmmm... can you dumb it down a bit?
A formula in mathmatics not needing a computer?
Just so i can do it by hand.

And in layman''s terms please, i am only good at up to grade 11 math, and then i stopped taking it.

Even for my game, we''ve got a guy on our team known as our Math Consultant

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 11
• 9
• 9
• 44
• ### Forum Statistics

• Total Topics
634131
• Total Posts
3015720
×