OSDN Git Service

added a file which explains something important
[proj16/16.git] / src / util / db.c
1 #include <stdio.h>      /* printf */
2 #include <string.h>     /* strcat */
3 #include <stdlib.h>     /* strtol */
4
5 const char *byte_to_binary(int x)
6 {
7         static char b[9];
8         int z;
9
10         b[0] = '\0';
11         for (z = 128; z > 0; z >>= 1)
12         {
13                 strcat(b, ((x & z) == z) ? "1" : "0");
14         }
15         return b;
16 }
17
18 void main()
19 {
20         /* binary string to int */
21         char *tmp;
22         char *b = "1101";
23
24         printf("%d\n", strtol(b, &tmp, 2));
25         printf("%x\n", strtol(b, &tmp, 2));
26
27         /* byte to binary string */
28         printf("%s\n", byte_to_binary(4));
29         printf("%s\n", byte_to_binary(16));
30 //    return 0;
31 }