1 #include <stdio.h> /* printf */
2 #include <string.h> /* strcat */
3 #include <stdlib.h> /* strtol */
5 const char *byte_to_binary(int x)
11 for (z = 128; z > 0; z >>= 1)
13 strcat(b, ((x & z) == z) ? "1" : "0");
20 /* binary string to int */
24 printf("%d\n", strtol(b, &tmp, 2));
25 printf("%x\n", strtol(b, &tmp, 2));
27 /* byte to binary string */
28 printf("%s\n", byte_to_binary(4));
29 printf("%s\n", byte_to_binary(16));