Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!uwvax!oddjob!gargoyle!ihnp4!pegasus!hansen From: hansen@pegasus.UUCP Newsgroups: comp.lang.c,net.sources Subject: Re: binary radix (+ some source) Message-ID: <2970@pegasus.UUCP> Date: Thu, 16-Apr-87 13:51:25 EST Article-I.D.: pegasus.2970 Posted: Thu Apr 16 13:51:25 1987 Date-Received: Sat, 18-Apr-87 05:14:42 EST References: <213@pyuxe.UUCP> <710@brl-sem.ARPA> <422@ivax.doc.ic.ac.uk> Reply-To: hansen@pegasus.UUCP (60021254-Tony L. Hansen;LZ 3B-315;6243) Organization: AT&T Information Systems, Lincroft, NJ Lines: 124 Keywords: octal, hex, binary Xref: utgpu comp.lang.c:1655 net.sources:6524 Summary: oct+hex+bin printing program < From: dcw@doc.ic.ac.uk (Duncan C White) < 2). In Article 1155 of comp.lang.c, bright@dataio.Data-IO.COM, says : < > How about allowing numbers to be specified in binary radix, < > as in 0b00110101 ? No existing code would be broken, and it < > would be very useful to those of us writing graphics code, < > ... < > As a corollary, support %b in printf and scanf. < < I agree completely. I too agree. ...open toolbox...rummage, rummage...ahh, here's an occassionally handy tool right here that I wrote quite some time ago. /* Convert the numeric arguments into decimal, octal, hexadecimal, and binary. A octal number is prefixed with "0". A hex number is prefixed with "0x" or "0X". A binary number is prefixed with "0b" or "0B". */ Enjoy! Tony Hansen ihnp4!pegasus!hansen P.S. Some notes on portability: This program makes use of two non-universally available features. The first is strtol(), which is very similar to atol() except that it can be set to check the first two characters for "0", "0x" or "0X" and adjust the base accordingly. Public domain versions of this routine have been posted in the past. An alternative is to write your own. Another alternative is to check for these prefixes separately and parallel btol() with otol() and xtol(). The second is the value HIBITL taken from . This is simply a long with the highest bit turned on. One way of getting it is to use: #define HIBITL ((unsigned long)(~((unsigned long)~0>>1))) Some people may be able to find a similar value in . #!/bin/sh # This is a shar archive. # The rest of this file is a shell script which will extract: # # octhex.c # # To extract the files from this shell archive file simply # create a directory for this file, move the archive file # to it and enter the command # # sh filename # # The files will be extracted automatically. # Note: Do not use csh. # # Archive created: Thu Apr 16 14:37:22 EDT 1987 # echo x - octhex.c sed 's/^X//' > octhex.c << '~FUNKY STUFF~' /* Convert the numeric arguments into decimal, octal, hexadecimal, and binary. A octal number is prefixed with "0". A hex number is prefixed with "0x" or "0X". A binary number is prefixed with "0b" or "0B". */ #include #include long strtol(); /* print out the number */ void prnum(i) long i; { unsigned long mask; printf("%d 0%o 0x%x 0b", i, i, i); /* print a number as a bit pattern */ for (mask = HIBITL; mask != 0; mask >>= 1) if ((i & mask) != 0) putchar('1'); else putchar('.'); putchar('\n'); } /* convert a number specified by a bit pattern into a long. */ long btol(s) char *s; { unsigned long i = 0; for (s += 2; *s; s++) { i <<= 1; if (*s == '1') i |= 1; } return i; } main(argc,argv) int argc; char **argv; { while (--argc) { char *num = *++argv; if (num[0] == '0' && (num[1] == 'b' || num[1] == 'B')) prnum(btol(num)); else prnum(strtol(num, (char**)NULL, 0)); } return 0; } ~FUNKY STUFF~ ls -l octhex.c # The following exit is to ensure that extra garbage # after the end of the shar file will be ignored. exit 0