Is there a generic formula for converting Decimal into any number base (except maybe HEX cause of the ABCDEF stuff)? I know the Decimal->Binary one, but it only works for that...
If you know, tell me! Or a link to a site would be good too.
+H3 FU+UR3 ØF MUZ!K MUZ+ NØ+ B37ØNG +Ø +H3 m3D!ØCr3
"I couldn't break the rules if we didn't have any." - Me on discussing why anarchy is better than democracy in History class.
Number Bases
..a function from a c++ book of mine
you can probably just take the thoery behind it
Edited by - Quantum on September 10, 2000 2:30:16 AM
string Convert(const int #, const int &newbase){ string str; char ch; int remainder=0; int quotient=num; while(quotient>0) { remainder = quotient % newbase; quotient = quotient / newbase; itoa(remainder, &ch, 10); str+=ch; } str=ReverseStr(str); //reverse the string because the bits in the number are converted from low to high return str;}
you can probably just take the thoery behind it
Edited by - Quantum on September 10, 2000 2:30:16 AM
That probably doesn''t work for Hex, does it?
+H3 FU+UR3 ØF MUZ!K MUZ+ NØ+ B37ØNG +Ø +H3 m3D!ØCr3
"I couldn't break the rules if we didn't have any." - Me on discussing why anarchy is better than democracy in History class.
+H3 FU+UR3 ØF MUZ!K MUZ+ NØ+ B37ØNG +Ø +H3 m3D!ØCr3
"I couldn't break the rules if we didn't have any." - Me on discussing why anarchy is better than democracy in History class.
"Now watch as I run away in a womanly fashion." - Batman
Sponge ... you can make that algorithm work for ANY number system, in ANY character set ... all you have to do is write a really small and simple function to put in place of itoa (but remember this function doesn''t really need to be itoa ... it only need to be ONE digit to one character ... otherwise you would just have used itoa to do the whole damn thing .
So .. example say I want to do hex ...then I write a function called say ..
char HexDigitToChar(int digit)
which returns ''0''..''9'',''A''..''F'' or throws an exception if out_of_range.
then the itoa line becomes:
HexDigitToChar(remainder);
and your done...
not ... his convert function could EASILY be made templated ... taking the base and the digit conversion function as template parameters. Plenty of other tricks to speed it up too.
BTW ... one quick way to speed his example up is simply to replace itoa(remainder,&ch,10) with the following:
ch = ''0'' + remainder;
which has the added benifit of removing the functions one bug - note that the current itoa is writting the ''\0'' character one byte past the address of ch ... which is NOT correct
but that could have been fixed by having ch declared as:
char ch[2]; and simple using ch instead of &ch and ch[0] instead of ch;
ALSO .. itoa(remainder,ch,16); would convert to any of the folowoing bases for sure - 2,4,8,16 ... not sure about non powers of two ... the itoa docs say any radix 2-36 ... so that would mean that you could do this:
itoa(remainder,ch,36); and leverage all of the power of the built in function ... which i assume uses this set ''0''-''9'',''A''-''F''. which would be 36 characters.
note ... you could even do this ULTRA-FAST with the follwing lines:
if(remainder < 10)
ch = ''0'' + remainder
else if(remainder < 36)
ch = ''A'' + remainder;
else
;// ERROR
well .. i think that covers all the bases ... if you want to go higher than 36 .. you''ll need to decide on your own character encoding ... and then write a function which does the maping ... or simply allocate a constant global array ... like a map.
good luck
So .. example say I want to do hex ...then I write a function called say ..
char HexDigitToChar(int digit)
which returns ''0''..''9'',''A''..''F'' or throws an exception if out_of_range.
then the itoa line becomes:
HexDigitToChar(remainder);
and your done...
not ... his convert function could EASILY be made templated ... taking the base and the digit conversion function as template parameters. Plenty of other tricks to speed it up too.
BTW ... one quick way to speed his example up is simply to replace itoa(remainder,&ch,10) with the following:
ch = ''0'' + remainder;
which has the added benifit of removing the functions one bug - note that the current itoa is writting the ''\0'' character one byte past the address of ch ... which is NOT correct
but that could have been fixed by having ch declared as:
char ch[2]; and simple using ch instead of &ch and ch[0] instead of ch;
ALSO .. itoa(remainder,ch,16); would convert to any of the folowoing bases for sure - 2,4,8,16 ... not sure about non powers of two ... the itoa docs say any radix 2-36 ... so that would mean that you could do this:
itoa(remainder,ch,36); and leverage all of the power of the built in function ... which i assume uses this set ''0''-''9'',''A''-''F''. which would be 36 characters.
note ... you could even do this ULTRA-FAST with the follwing lines:
if(remainder < 10)
ch = ''0'' + remainder
else if(remainder < 36)
ch = ''A'' + remainder;
else
;// ERROR
well .. i think that covers all the bases ... if you want to go higher than 36 .. you''ll need to decide on your own character encoding ... and then write a function which does the maping ... or simply allocate a constant global array ... like a map.
good luck
Yeah, I knew about the itoa radix thing...
But otherwise, thanks for the help. Didn''t think about doing it with templates, but I really don''t know to much about them, except that they''re reall slow(?). I assume you can''t go over 36 because the alphabet runs out, how would you do it then? I don''t really need base 36 (who does) but it would be cool to be able to write in base 50 or something, haha.
Thanks for the help!
+H3 FU+UR3 ØF MUZ!K MUZ+ NØ+ B37ØNG +Ø +H3 m3D!ØCr3
"I couldn't break the rules if we didn't have any." - Me on discussing why anarchy is better than democracy in History class.
But otherwise, thanks for the help. Didn''t think about doing it with templates, but I really don''t know to much about them, except that they''re reall slow(?). I assume you can''t go over 36 because the alphabet runs out, how would you do it then? I don''t really need base 36 (who does) but it would be cool to be able to write in base 50 or something, haha.
Thanks for the help!
+H3 FU+UR3 ØF MUZ!K MUZ+ NØ+ B37ØNG +Ø +H3 m3D!ØCr3
"I couldn't break the rules if we didn't have any." - Me on discussing why anarchy is better than democracy in History class.
"Now watch as I run away in a womanly fashion." - Batman
The trick to going above base 36 .. is to define the rules for output, THEN simply right a function that follows them. For example, notice that people constantly use two hex characters to refer to a byte, and they NEVER just use the one character when they COULD get away with it (0x0F could be 0xF), so you could say they are actaully using base 256, not base 16 (because you must use multiples of bytes, not an odd number of nibbles), and if you think of it as base 256, you reaelize it is simple that each digit is 2 characters wide (just like 16 bit unicode .
SO ... IF you can think of a resonable system with 100 letters, you can use base 100 .. but in reality we already have the ability just by combining 2 base 10 digits, SO when you are really interested in NEW bases, they must not be factorable into lower bases. (base 8 and 16 have differences BUT neither has any power not in base 2 besides compactness of input and output - base 10 contains all the power and ability of base 1000, which by the way is the base which contains the following digits [ones, thousands, millions, billions, trilions, etc.] So, that should give you something to think about. Personally I find only a few bases interesting: 1(totally different than all others - and contains no zero number),2,3,6,5,7,10,12,36 (but you could get more wierd ones by combining or simply choosing prime numbers ...) good luck.
I did once create a set of 8 graphic images and use them to output common computer numbers (like 0x8080 and 0x5555) in octal (which looked strange due to diff alphebet).
SO ... IF you can think of a resonable system with 100 letters, you can use base 100 .. but in reality we already have the ability just by combining 2 base 10 digits, SO when you are really interested in NEW bases, they must not be factorable into lower bases. (base 8 and 16 have differences BUT neither has any power not in base 2 besides compactness of input and output - base 10 contains all the power and ability of base 1000, which by the way is the base which contains the following digits [ones, thousands, millions, billions, trilions, etc.] So, that should give you something to think about. Personally I find only a few bases interesting: 1(totally different than all others - and contains no zero number),2,3,6,5,7,10,12,36 (but you could get more wierd ones by combining or simply choosing prime numbers ...) good luck.
I did once create a set of 8 graphic images and use them to output common computer numbers (like 0x8080 and 0x5555) in octal (which looked strange due to diff alphebet).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement