Advertisement

Counting Unit Conversion

Started by April 19, 2002 04:24 PM
14 comments, last by black_mage_s 22 years, 9 months ago
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
"Luck is for people without skill."- Robert (I Want My Island)"Real men eat food that felt pain before it died."- Me
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
Advertisement
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]
I know that I don't know nothing... Operation Ivy
ummmm... i was thinking more along the lines of :
1 2 3 4 5 6 7 10
"Luck is for people without skill."- Robert (I Want My Island)"Real men eat food that felt pain before it died."- Me
What ?
I know that I don't know nothing... Operation Ivy
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...

Advertisement
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]
I know that I don't know nothing... Operation Ivy
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.
Keys to success: Ability, ambition and opportunity.
#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, NULL, 10);<br>        ltoa(n, str, 8);<br>        printf("%s base 10 = %s base 8\n", argv, str);<br>    }<br><br>    return 0;<br>}<br> </pre>  <br><br><br><SPAN CLASS=editedby>[edited by - Jeff K on April 19, 2002 11:35:27 PM]</SPAN>    
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
"Luck is for people without skill."- Robert (I Want My Island)"Real men eat food that felt pain before it died."- Me

This topic is closed to new replies.

Advertisement