Thread View: borland.public.cppbuilder.nativeapi
7 messages
7 total messages
Started by jB
Wed, 27 Feb 2008 10:27
Base10->Base32->Base10 Help Please
Author: jB
Date: Wed, 27 Feb 2008 10:27
Date: Wed, 27 Feb 2008 10:27
11 lines
1137 bytes
1137 bytes
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
Author: Jonathan Benedicto
Date: Wed, 27 Feb 2008 15:35
Date: Wed, 27 Feb 2008 15:35
17 lines
1351 bytes
1351 bytes
jB wrote: > 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
Author: Clayton Arends
Date: Wed, 27 Feb 2008 16:22
Date: Wed, 27 Feb 2008 16:22
25 lines
2284 bytes
2284 bytes
"jB" <jb@britesc.com> wrote in message news:47c53b22@newsgroups.borland.com... > 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 http://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 http://en.wikipedia.org/wiki/Base32 Clayton
Re: Base10->Base32->Base10 Help Please
Author: Clayton Arends
Date: Wed, 27 Feb 2008 16:23
Date: Wed, 27 Feb 2008 16:23
11 lines
1379 bytes
1379 bytes
"Jonathan Benedicto" <invalid@nobody.com> wrote in message news:47c58914$1@newsgroups.borland.com... > 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
Author: Alan Bellingham
Date: Wed, 27 Feb 2008 16:37
Date: Wed, 27 Feb 2008 16:37
12 lines
1227 bytes
1227 bytes
"Clayton Arends" <nospam_claytonarends@hotmail.com> wrote: >> 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
Author: Jonathan Benedicto
Date: Wed, 27 Feb 2008 16:48
Date: Wed, 27 Feb 2008 16:48
6 lines
1072 bytes
1072 bytes
Clayton Arends wrote: > The OP wants a Base32 algorithm not Base16 Doh! Thanks. Jon
Re: Base10->Base32->Base10 Help Please
Author: Dennis Cote
Date: Wed, 27 Feb 2008 17:04
Date: Wed, 27 Feb 2008 17:04
76 lines
2750 bytes
2750 bytes
jB wrote:
>
> 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;
}
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads