Board index » cppbuilder » Base10->Base32->Base10 Help Please
jB
![]() CBuilder Developer |
jB
![]() CBuilder Developer |
Base10->Base32->Base10 Help Please2008-02-27 06:27:31 PM cppbuilder32 Hi, Could anyone please help me with a couple of small routines to convert a base10 number into a base32 string and a routine to convert base32 string into a base10 number. A class would be great. Thanks jB |
Jonathan Benedicto
![]() CBuilder Developer |
2008-02-27 11:35:40 PM
Re:Base10->Base32->Base10 Help Please
jB wrote:
QuoteCould anyone please help me with a couple of small routines to convert a characters. int i = StrToInt( "0x10" ); HTH Jon |
Clayton Arends
![]() CBuilder Developer |
2008-02-28 12:22:20 AM
Re:Base10->Base32->Base10 Help Please
"jB" < XXXX@XXXXX.COM >wrote in message
QuoteCould anyone please help me with a couple of small routines to convert a (and you should use the .students newsgroup). Either way there are *plenty* of examples already out there. You just need to use a search engine. If you want to search previous newsgroup postings then use groups.google.com and search for something like "base32 base10 conversion". The core of the algorithm is that you will take your number and continually divide it by 32 (taking the remainder as your string characters) until you are left with 0. When converting the other way you will do the opposite, multiply your number by 32 and add the character from the string until the string is empty. The characters in the string will use an alphabet of your choosing. There are several "standards". To see a good write-up on this go to en.wikipedia.org/wiki/Base32 Clayton {smallsort} |
Clayton Arends
![]() CBuilder Developer |
2008-02-28 12:23:42 AM
Re:Base10->Base32->Base10 Help Please
"Jonathan Benedicto" < XXXX@XXXXX.COM >wrote in message
QuoteString Str = IntToHex( 123456, 2 ); // 2nd parameter is pad to how many |
Alan Bellingham
![]() CBuilder Developer |
2008-02-28 12:37:17 AM
Re:Base10->Base32->Base10 Help Please
"Clayton Arends" < XXXX@XXXXX.COM >wrote:
Quote>int i = StrToInt( "0x10" ); -- Team Browns ACCU Conference 2008: 2-5 April 2008 - Oxford, UK |
Jonathan Benedicto
![]() CBuilder Developer |
2008-02-28 12:48:38 AM
Re:Base10->Base32->Base10 Help Please
Clayton Arends wrote:
QuoteThe OP wants a Base32 algorithm not Base16 |
Dennis Cote
![]() CBuilder Developer |
2008-02-28 01:04:18 AM
Re:Base10->Base32->Base10 Help Please
jB wrote:
Quote
function to do the complementary conversion, at least that I am aware of. I have written a small function to do that long to string conversion using a base between 2 and 36. It is copied below. There is also a small command line test program that converts its first argument using the second argument as a base. These functions should let you do your conversions to and from base 32. HTH Dennis Cote #include <stdlib.h> #include <stdio.h> static char digit_symbols[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVXYZ"; bool ltostr(long n, char* str, int base) { char buf[33]; char* p = buf; if (!str || base < 2 || base>36) return false; if (n < 0) { *str++ = '-'; n = -n; } do { *p++ = digit_symbols[n % base]; n /= base; } while (n>0); do { *str++ = *(--p); } while (p>buf); *str = 0; return true; } #pragma argsused int main(int argc, char* argv[]) { char str[33]; long m; long n = atol(argv[1]); int base = atoi(argv[2]); printf("n = %ld base 10\n", n); if (ltostr(n, str, base)) printf("n = %s base %d\n", str, base); else printf("error converting n\n"); m = strtol(str, NULL, base); printf("m = %ld base 10\n", m); return 0; } |