Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,890 questions

51,821 answers

573 users

How to display the binary and Hex table for of all 8bit (0 - 255) numbers in C

1 Answer

0 votes
#include <stdio.h>

char *toBinFormat(int n);

int main(int argc, char **argv) 
{ 
    printf("Dec | Hex    | Bin      - Dec | Hex    | Bin      - Dec | Hex    | Bin\n");
    for (int i = 0; i < 256; i++)
    {
        printf("%3d | 0x%04X | %s - ", i, i, toBinFormat(i));
        if ((i + 1) % 3 == 0) printf("\n");
            
    }
    
    return(0);
}

char *toBinFormat(int n)
{
    static char binary_value[9]; // without static binary_value is a local 
                                 // array that disappear after return
    int i;
    
    for(i = 0; i < 8; i++)
    {
        binary_value[i] = n & 0x80 ? '1' : '0';
        n <<= 1;
    }
    binary_value[i] = '\0';

    return binary_value;
}

/*
run:

Dec | Hex    | Bin      - Dec | Hex    | Bin      - Dec | Hex    | Bin
  0 | 0x0000 | 00000000 -   1 | 0x0001 | 00000001 -   2 | 0x0002 | 00000010 -
  3 | 0x0003 | 00000011 -   4 | 0x0004 | 00000100 -   5 | 0x0005 | 00000101 -
  6 | 0x0006 | 00000110 -   7 | 0x0007 | 00000111 -   8 | 0x0008 | 00001000 -
  9 | 0x0009 | 00001001 -  10 | 0x000A | 00001010 -  11 | 0x000B | 00001011 -
 12 | 0x000C | 00001100 -  13 | 0x000D | 00001101 -  14 | 0x000E | 00001110 -
 15 | 0x000F | 00001111 -  16 | 0x0010 | 00010000 -  17 | 0x0011 | 00010001 -
 18 | 0x0012 | 00010010 -  19 | 0x0013 | 00010011 -  20 | 0x0014 | 00010100 -
 21 | 0x0015 | 00010101 -  22 | 0x0016 | 00010110 -  23 | 0x0017 | 00010111 -
 24 | 0x0018 | 00011000 -  25 | 0x0019 | 00011001 -  26 | 0x001A | 00011010 -
 27 | 0x001B | 00011011 -  28 | 0x001C | 00011100 -  29 | 0x001D | 00011101 -
 30 | 0x001E | 00011110 -  31 | 0x001F | 00011111 -  32 | 0x0020 | 00100000 -
 33 | 0x0021 | 00100001 -  34 | 0x0022 | 00100010 -  35 | 0x0023 | 00100011 -
 36 | 0x0024 | 00100100 -  37 | 0x0025 | 00100101 -  38 | 0x0026 | 00100110 -
 39 | 0x0027 | 00100111 -  40 | 0x0028 | 00101000 -  41 | 0x0029 | 00101001 -
 42 | 0x002A | 00101010 -  43 | 0x002B | 00101011 -  44 | 0x002C | 00101100 -
 45 | 0x002D | 00101101 -  46 | 0x002E | 00101110 -  47 | 0x002F | 00101111 -
 48 | 0x0030 | 00110000 -  49 | 0x0031 | 00110001 -  50 | 0x0032 | 00110010 -
 51 | 0x0033 | 00110011 -  52 | 0x0034 | 00110100 -  53 | 0x0035 | 00110101 -
 54 | 0x0036 | 00110110 -  55 | 0x0037 | 00110111 -  56 | 0x0038 | 00111000 -
 57 | 0x0039 | 00111001 -  58 | 0x003A | 00111010 -  59 | 0x003B | 00111011 -
 60 | 0x003C | 00111100 -  61 | 0x003D | 00111101 -  62 | 0x003E | 00111110 -
 63 | 0x003F | 00111111 -  64 | 0x0040 | 01000000 -  65 | 0x0041 | 01000001 -
 66 | 0x0042 | 01000010 -  67 | 0x0043 | 01000011 -  68 | 0x0044 | 01000100 -
 69 | 0x0045 | 01000101 -  70 | 0x0046 | 01000110 -  71 | 0x0047 | 01000111 -
 72 | 0x0048 | 01001000 -  73 | 0x0049 | 01001001 -  74 | 0x004A | 01001010 -
 75 | 0x004B | 01001011 -  76 | 0x004C | 01001100 -  77 | 0x004D | 01001101 -
 78 | 0x004E | 01001110 -  79 | 0x004F | 01001111 -  80 | 0x0050 | 01010000 -
 81 | 0x0051 | 01010001 -  82 | 0x0052 | 01010010 -  83 | 0x0053 | 01010011 -
 84 | 0x0054 | 01010100 -  85 | 0x0055 | 01010101 -  86 | 0x0056 | 01010110 -
 87 | 0x0057 | 01010111 -  88 | 0x0058 | 01011000 -  89 | 0x0059 | 01011001 -
 90 | 0x005A | 01011010 -  91 | 0x005B | 01011011 -  92 | 0x005C | 01011100 -
 93 | 0x005D | 01011101 -  94 | 0x005E | 01011110 -  95 | 0x005F | 01011111 -
 96 | 0x0060 | 01100000 -  97 | 0x0061 | 01100001 -  98 | 0x0062 | 01100010 -
 99 | 0x0063 | 01100011 - 100 | 0x0064 | 01100100 - 101 | 0x0065 | 01100101 -
102 | 0x0066 | 01100110 - 103 | 0x0067 | 01100111 - 104 | 0x0068 | 01101000 -
105 | 0x0069 | 01101001 - 106 | 0x006A | 01101010 - 107 | 0x006B | 01101011 -
108 | 0x006C | 01101100 - 109 | 0x006D | 01101101 - 110 | 0x006E | 01101110 -
111 | 0x006F | 01101111 - 112 | 0x0070 | 01110000 - 113 | 0x0071 | 01110001 -
114 | 0x0072 | 01110010 - 115 | 0x0073 | 01110011 - 116 | 0x0074 | 01110100 -
117 | 0x0075 | 01110101 - 118 | 0x0076 | 01110110 - 119 | 0x0077 | 01110111 -
120 | 0x0078 | 01111000 - 121 | 0x0079 | 01111001 - 122 | 0x007A | 01111010 -
123 | 0x007B | 01111011 - 124 | 0x007C | 01111100 - 125 | 0x007D | 01111101 -
126 | 0x007E | 01111110 - 127 | 0x007F | 01111111 - 128 | 0x0080 | 10000000 -
129 | 0x0081 | 10000001 - 130 | 0x0082 | 10000010 - 131 | 0x0083 | 10000011 -
132 | 0x0084 | 10000100 - 133 | 0x0085 | 10000101 - 134 | 0x0086 | 10000110 -
135 | 0x0087 | 10000111 - 136 | 0x0088 | 10001000 - 137 | 0x0089 | 10001001 -
138 | 0x008A | 10001010 - 139 | 0x008B | 10001011 - 140 | 0x008C | 10001100 -
141 | 0x008D | 10001101 - 142 | 0x008E | 10001110 - 143 | 0x008F | 10001111 -
144 | 0x0090 | 10010000 - 145 | 0x0091 | 10010001 - 146 | 0x0092 | 10010010 -
147 | 0x0093 | 10010011 - 148 | 0x0094 | 10010100 - 149 | 0x0095 | 10010101 -
150 | 0x0096 | 10010110 - 151 | 0x0097 | 10010111 - 152 | 0x0098 | 10011000 -
153 | 0x0099 | 10011001 - 154 | 0x009A | 10011010 - 155 | 0x009B | 10011011 -
156 | 0x009C | 10011100 - 157 | 0x009D | 10011101 - 158 | 0x009E | 10011110 -
159 | 0x009F | 10011111 - 160 | 0x00A0 | 10100000 - 161 | 0x00A1 | 10100001 -
162 | 0x00A2 | 10100010 - 163 | 0x00A3 | 10100011 - 164 | 0x00A4 | 10100100 -
165 | 0x00A5 | 10100101 - 166 | 0x00A6 | 10100110 - 167 | 0x00A7 | 10100111 -
168 | 0x00A8 | 10101000 - 169 | 0x00A9 | 10101001 - 170 | 0x00AA | 10101010 -
171 | 0x00AB | 10101011 - 172 | 0x00AC | 10101100 - 173 | 0x00AD | 10101101 -
174 | 0x00AE | 10101110 - 175 | 0x00AF | 10101111 - 176 | 0x00B0 | 10110000 -
177 | 0x00B1 | 10110001 - 178 | 0x00B2 | 10110010 - 179 | 0x00B3 | 10110011 -
180 | 0x00B4 | 10110100 - 181 | 0x00B5 | 10110101 - 182 | 0x00B6 | 10110110 -
183 | 0x00B7 | 10110111 - 184 | 0x00B8 | 10111000 - 185 | 0x00B9 | 10111001 -
186 | 0x00BA | 10111010 - 187 | 0x00BB | 10111011 - 188 | 0x00BC | 10111100 -
189 | 0x00BD | 10111101 - 190 | 0x00BE | 10111110 - 191 | 0x00BF | 10111111 -
192 | 0x00C0 | 11000000 - 193 | 0x00C1 | 11000001 - 194 | 0x00C2 | 11000010 -
195 | 0x00C3 | 11000011 - 196 | 0x00C4 | 11000100 - 197 | 0x00C5 | 11000101 -
198 | 0x00C6 | 11000110 - 199 | 0x00C7 | 11000111 - 200 | 0x00C8 | 11001000 -
201 | 0x00C9 | 11001001 - 202 | 0x00CA | 11001010 - 203 | 0x00CB | 11001011 -
204 | 0x00CC | 11001100 - 205 | 0x00CD | 11001101 - 206 | 0x00CE | 11001110 -
207 | 0x00CF | 11001111 - 208 | 0x00D0 | 11010000 - 209 | 0x00D1 | 11010001 -
210 | 0x00D2 | 11010010 - 211 | 0x00D3 | 11010011 - 212 | 0x00D4 | 11010100 -
213 | 0x00D5 | 11010101 - 214 | 0x00D6 | 11010110 - 215 | 0x00D7 | 11010111 -
216 | 0x00D8 | 11011000 - 217 | 0x00D9 | 11011001 - 218 | 0x00DA | 11011010 -
219 | 0x00DB | 11011011 - 220 | 0x00DC | 11011100 - 221 | 0x00DD | 11011101 -
222 | 0x00DE | 11011110 - 223 | 0x00DF | 11011111 - 224 | 0x00E0 | 11100000 -
225 | 0x00E1 | 11100001 - 226 | 0x00E2 | 11100010 - 227 | 0x00E3 | 11100011 -
228 | 0x00E4 | 11100100 - 229 | 0x00E5 | 11100101 - 230 | 0x00E6 | 11100110 -
231 | 0x00E7 | 11100111 - 232 | 0x00E8 | 11101000 - 233 | 0x00E9 | 11101001 -
234 | 0x00EA | 11101010 - 235 | 0x00EB | 11101011 - 236 | 0x00EC | 11101100 -
237 | 0x00ED | 11101101 - 238 | 0x00EE | 11101110 - 239 | 0x00EF | 11101111 -
240 | 0x00F0 | 11110000 - 241 | 0x00F1 | 11110001 - 242 | 0x00F2 | 11110010 -
243 | 0x00F3 | 11110011 - 244 | 0x00F4 | 11110100 - 245 | 0x00F5 | 11110101 -
246 | 0x00F6 | 11110110 - 247 | 0x00F7 | 11110111 - 248 | 0x00F8 | 11111000 -
249 | 0x00F9 | 11111001 - 250 | 0x00FA | 11111010 - 251 | 0x00FB | 11111011 -
252 | 0x00FC | 11111100 - 253 | 0x00FD | 11111101 - 254 | 0x00FE | 11111110 -
255 | 0x00FF | 11111111 -


*/

 



answered Jun 15, 2015 by avibootz

Related questions

...