Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!njin!princeton!phoenix.Princeton.EDU!pfalstad From: pfalstad@phoenix.Princeton.EDU (Paul Falstad) Newsgroups: comp.lang.c Subject: Re: Converting between bases Message-ID: <6749@idunno.Princeton.EDU> Date: 1 Mar 91 06:00:22 GMT References: <62375@eerie.acsu.Buffalo.EDU> Sender: news@idunno.Princeton.EDU Distribution: usa Organization: The Royal Society For Putting Things On Top Of Other Things Lines: 47 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.