Board index » cppbuilder » Base10->Base32->Base10 Help Please

Base10->Base32->Base10 Help Please


2008-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
 
 

Re:Base10->Base32->Base10 Help Please

jB wrote:
Quote
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.
If you're using the VCL:
String Str = IntToHex( 123456, 2 ); // 2nd parameter is pad to how many
characters.
int i = StrToInt( "0x10" );
HTH
Jon
 

Re:Base10->Base32->Base10 Help Please

"jB" < XXXX@XXXXX.COM >wrote in message
Quote
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.
You didn't answer Jason's speculation (in language.cpp) that this is
homework. If it is homework then you would learn more by doing it yourself
(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}

Re:Base10->Base32->Base10 Help Please

"Jonathan Benedicto" < XXXX@XXXXX.COM >wrote in message
Quote
String Str = IntToHex( 123456, 2 ); // 2nd parameter is pad to how many
characters.

int i = StrToInt( "0x10" );
The OP wants a Base32 algorithm not Base16
Clayton
 

Re:Base10->Base32->Base10 Help Please

"Clayton Arends" < XXXX@XXXXX.COM >wrote:
Quote
>int i = StrToInt( "0x10" );

The OP wants a Base32 algorithm not Base16
So do it twice!
Alan Bellingham
--
Team Browns
ACCU Conference 2008: 2-5 April 2008 - Oxford, UK
 

Re:Base10->Base32->Base10 Help Please

Clayton Arends wrote:
Quote
The OP wants a Base32 algorithm not Base16
Doh! Thanks.
Jon
 

Re:Base10->Base32->Base10 Help Please

jB wrote:
Quote

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.

The C standard library has the strtol() function which will convert a
string to a long using a base between 2 and 36. There is no standard
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;
}