• Advertisement

Archived

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

Counting Unit Conversion

This topic is 5751 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
quote:
Does anyone here know how to convert from a base 10 counting system to a base 8? And vice versa?
Yeah :
quote:
decimal to octal :

1457 = 182 * 8 + 1
182 = 22 * 8 + 6
22 = 2 * 8 + 6
2 = 0 * 8 + 2

Thus 1457d = 2661o

octal to decimal :

2661o = 2*8^3 + 6*8^2 + 6*8^1 + 1*8^0 = 1457d
quote:
Just so i can do it by hand.
Isn't it a "by hand" example?

[edited by - Bloodscourge on April 21, 2002 7:32:20 AM]

Share this post


Link to post
Share on other sites
Another example :

decimal to octal :

31057 = 3882 * 8 + 1
3882 = 485 * 8 + 2
485 = 60 * 8 + 5
60 = 7 * 8 + 4
7 = 0 * 8 + 7

Then 31057d = 74521o

octal to decimal :

74521o = 7*8^4 + 4*8^3 + 5*8^2 + 2*8^1 + 1*8^0 = 31057d

Got it?

[edited by - Bloodscourge on April 21, 2002 7:47:55 AM]

Share this post


Link to post
Share on other sites
Another way to see the decimal to octal conversion :

31057 | 8
------+------
1 | 3882 | 8
|------+------
| 2 | 485 | 8
|------+------
| 5 | 60 | 8
|------+------
| 4 | 7 | 8
|------+------ <---- this part not necessary
| 7 | 0
|
It' clear that it's a chain of division by 8. Read in reversed order the remainders and you have your base 8 number -> 74521o

[edited by - Bloodscourge on April 21, 2002 8:23:36 AM]

Share this post


Link to post
Share on other sites
If you don''t need your program to do it, then why not just use a calculator that can do the conversion (such as the one that comes with Windows (if that''s the OS you''re using)).

Share this post


Link to post
Share on other sites
A modulus divide is just taking the remainder on a division. So 17/8 is 2+1/8 and the remainder on the division is 1.

Share this post


Link to post
Share on other sites
I really like the idea of a society which speaks in octal!

If you''re using C...

printf("%o in base 8 is %d in base ten",n,n);

You can also use scanf to input an octal number. This seems easier than the other methods.

Share this post


Link to post
Share on other sites

  • Advertisement