Binary Chart C Program

From BenningtonWiki
Jump to: navigation, search

<source lang="C">

  1. include <stdio.h>

void convert_to_binary_string(int b, char *s);

int main(void) {

int b; char str[10]; char ch[2];

ch[1] = '\0';

for (b = 0; b < 256; ++b) {

convert_to_binary_string(b, str); if (b >= 32 && b <= 126) ch[0] = b; else ch[0] = ' '; printf("%s%8d%8s\n", str, b, ch); if ((b % 16) == 15) printf("\n"); }

}


void convert_to_binary_string(int b, char *s) {

int i, m;

for (i = 0, m = 128; i < 8; ++i, m >>= 1) {

s[i] = (b & m) ? '1' : '0';

}

s[8] = '\0';

} </source>