Article View: borland.public.cppbuilder.nativeapi
Article #565Re: Base10->Base32->Base10 Help Please
From: 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;
}
Technical Details
Message-ID:
<47c59810$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder3.cambrium.nl!feeder1.cambrium.nl!feed.tweaknews.nl!193.141.40.65.MISMATCH!npeer.de.kpn-eurorings.net!npeer-ng1.kpn.DE!newsfeeder.dynfx.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<47c53b22@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,d1e84eddc30a90a X-Google-Attributes: gid10cc67,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Date: Wed, 27 Feb 2008 10:04:18 -0700 From: Dennis Cote <denniscote@gmail.com> User-Agent: Thunderbird 2.0.0.9 (Windows/20071031) MIME-Version: 1.0 Newsgroups: borland.public.cppbuilder.nativeapi Subject: Re: Base10->Base32->Base10 Help Please References: <47c53b22@newsgroups.borland.com> In-Reply-To: <47c53b22@newsgroups.borland.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 142.179.167.5 Message-ID: <47c59810$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1204131856 142.179.167.5 (27 Feb 2008 09:04:16 -0700) Lines: 76 Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder3.cambrium.nl!feeder1.cambrium.nl!feed.tweaknews.nl!193.141.40.65.MISMATCH!npeer.de.kpn-eurorings.net!npeer-ng1.kpn.DE!newsfeeder.dynfx.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail