Jump to content
  • Advertisement

Archived

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

black_mage_s

Counting Unit Conversion

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Ahhhhh, OK!

You mean :
 1
2
3
4
5
6
7
10
11
12
13
14
15
16
17
20
.
.
.


Sorry for the laaaaaaaaaaate understanding!

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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!