[comp.lang.c] Converting between bases

cloos@acsu.buffalo.edu (James H. Cloos) (02/28/91)

Hello.

I need to be able to convert arbitrarily long strings which a guaenteed to
match one of the regexps below (each one gets a different function) and
output the hex equivilent (again in a string) optionally cut down to a
specified number of hex digits.  (The least significant ones.)  The latter
part I can take care of, but the former is giving me trouble.

[01]+
[0-7]+
[0-9]+

If someone could give me a jab into the right direction (either what to do
or where to look) I'd be most grateful.  

Happy coding,

-JimC
--
James H. Cloos, Jr.		Phone:  +1 716 673-1250
cloos@ACSU.Buffalo.EDU		Snail:  PersonalZipCode:  14048-0772, USA
cloos@ub.UUCP			Quote:  <>

pfalstad@phoenix.Princeton.EDU (Paul Falstad) (03/01/91)

cloos@acsu.buffalo.edu (James H. Cloos) wrote:
>I need to be able to convert arbitrarily long strings which a guaenteed to
>match one of the regexps below (each one gets a different function) and
>output the hex equivilent (again in a string) optionally cut down to a
>specified number of hex digits.  (The least significant ones.)  The latter
>part I can take care of, but the former is giving me trouble.
>
>[01]+
>[0-7]+
>[0-9]+

strtol(3).

long binarytoi(char *s)
{
   return strtol(s,NULL,2);
}

long octaltoi(char *s)
{
   return strtol(s,NULL,8);
}

..

If you don't have strtol(3), here's a crippled version that will work
here: (hex conversions, as well as the base == 0 case, are left as an
exercise for the reader)

long strtol(char *s,char **x,int base)
{
long z;

   while (*s >= '0' && *s < '0'+base)
      z = z*base+(*s++-'0');
   if (x)
      *x = s;
   return z;
}

You can print hex with printf("%x\n",foo).

--
Paul Falstad, pfalstad@phoenix.princeton.edu PLink:HYPNOS GEnie:P.FALSTAD
How DO you delete a file called "-"?  For viewers at home, the answer is
coming up on your screen.  For those of you who wish to play it the hard
way, stand upside down with your head in a bucket of piranha fish.

rjohnson@shell.com (Roy Johnson) (03/07/91)

In article <62375@eerie.acsu.Buffalo.EDU> cloos@acsu.buffalo.edu (James H. Cloos) writes:
>Hello.

>I need to be able to convert arbitrarily long strings which a guaenteed to
>match one of the regexps below (each one gets a different function) and
>output the hex equivilent (again in a string) optionally cut down to a
>specified number of hex digits.  (The least significant ones.)  The latter
>part I can take care of, but the former is giving me trouble.

>[01]+
>[0-7]+
>[0-9]+

>If someone could give me a jab into the right direction (either what to do
>or where to look) I'd be most grateful.  

The following will work if the strings are not so arbitrarily long that
the machine cannot numerically represent them:

char hexstr[9];		/* The final result string */
char numstring[33];	/* The input string */
unsigned i;		/* Just a loop variable */
long num;		/* Numerical value of numstring */

Binary:
for(i=num=0; numstring[i] != '\0'; ++i) {
  num *= 2;
  if (numstring[i] == '1') ++num;
}
sprintf(hexstr, "%x", num);

Octal:
sscanf(numstring, "%o", &num);
sprintf(hexstr, "%x", num);

Decimal:
sscanf(numstring, "%d", &num);
sprintf(hexstr, "%x", num);

This looks suspiciously like homework, so I've purposely used
library routines, which most professors don't want you to use,
but which are fine more most real work.

--
======= !{sun,psuvax1,bcm,rice,decwrl,cs.utexas.edu}!shell!rjohnson =======
Feel free to correct me, but don't preface your correction with "BZZT!"
Roy Johnson, Shell Development Company