decimal to binary
Author Message
decimal to binary

Groovy hepcat Markus Schroeder was jivin' on Tue, 17 Dec 2002 15:13:30
+0100 in comp.lang.c.
decimal to binary's a cool scene! Dig it!

Quote:
>do you know those cool binary watches which tell you the time in binary

Nope, never heard of 'em.

Quote:
>format? I found one at www.thinkgeek.com, it is called "Matrix Binary
>Watch". Ohhhh I want this watch!!! :)

>To kill time, I decided to exercise reading binary time by writing a prog
>that tells you the time in binary format.
>OK.. I did most of the work, but now I'm stuck, because I don't know how to
>transform decimal into binary numbers... I think it would be the easiest if
>i use an array for the binary numbers, and modify the decimal numbers with
>the >> or << operator... but I have no idea, how to accomplish this.

The following code assumes CHAR_BIT == 8, but is flexible enough to
modify for any byte size.

/**************** BINBYTE2.C  ****************/
/* A demonstration of how to convert a value */
/* to a string of binary digits (bits).      */
/*********************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

#define ENDIAN_MACHINE
/* comment out if your system is big endian,
and leave uncommented for li'l endian. (Eg.,
don't comment out if you use an Intel based
machine.) */

/* NOTE: returns pointer to static data. This means it returns the
same address every time the function is called. Do not call this
function twice without using the return value (including all side
effects) in between the calls. */
char *binbyte(unsigned char b)
{
static char byte[9];
char *nibble[] =
{
"0000",
"0001",
"0010",
"0011",
"0100",
"0101",
"0110",
"0111",
"1000",
"1001",
"1010",
"1011",
"1100",
"1101",
"1110",
"1111"
};

strcpy(byte, nibble[b >> 4]);
strcat(byte, nibble[b & 0x0f]);

return byte;

Quote:
}

/* bininteger() works for any integer type */
char *bininteger(void *data, size_t size)
{
char *buf;
unsigned char *p = data;
int i;

if(data == NULL || (buf = malloc(size * (CHAR_BIT + 1))) == NULL)
return NULL;

*buf = '\0';

#ifdef ENDIAN_MACHINE
/* put bytes in the "right" order for li'l endian machines */
for(i = size - 1; i > 0; i--)
#else
for(i = 0; i < (size - 1); i++)
#endif
{
strcat(buf, binbyte(p[i]));
strcat(buf, " ");
}
strcat(buf, binbyte(p[i]));

return buf;

Quote:
}

/* binarray() works for char array */
char *binarray(char *data, size_t length)
{
char *buf;
size_t i;

if(data == NULL || (buf = malloc(length * (CHAR_BIT + 1))) == NULL)
return NULL;

*buf = '\0';

for(i = 0; i < (length - 1); i++)
{
strcat(buf, binbyte(data[i]));
strcat(buf, " ");
}
strcat(buf, binbyte(data[i]));

return buf;

Quote:
}

int main(void)
{
unsigned n;
char buf[100], *p, c;

printf("Enter a positive decimal number or a string\n"
"(or just press ENTER to quit): ");
fflush(stdout);

while(fgets(buf, sizeof buf, stdin) && *buf != '\n')
{
if(NULL != (p = strchr(buf, '\n')))
*p = '\0';  /* remove newline from the input */

/* test whether input is a number, and only a number -
else input is a string */
if(sscanf(buf, "%u%c", &n, &c) == 1)
{
/* number */
p = bininteger(&n, sizeof n);
if(NULL == p)
{
perror("Error converting to binary.\n");
return EXIT_FAILURE;
}
printf("That number in binary is: %s\n\n", p);
}
else
{
/* string */
p = binarray(buf, strlen(buf));
if(NULL == p)
{
fprintf(stderr, "Error converting to binary.\n");
return EXIT_FAILURE;
}
printf("That string in binary is:\n%s\n\n", p);
}

free(p);

printf("Enter a positive decimal number or a string\n"
"(or just press ENTER to quit): ");
fflush(stdout);
}

return 0;

Quote:
}

--

Dig the even newer still, yet more improved, sig!

http://www.*-*-*.com/ ~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?

Fri, 10 Jun 2005 07:37:04 GMT

 Page 1 of 1 [ 1 post ]

Relevant Pages