Skip to content

Instantly share code, notes, and snippets.

@ictlyh
Created November 30, 2016 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ictlyh/5f9b435b292dfa526be611f34d8cd5a9 to your computer and use it in GitHub Desktop.
Save ictlyh/5f9b435b292dfa526be611f34d8cd5a9 to your computer and use it in GitHub Desktop.
Demo of print bytes in binary
/*
* Demo of print bytes in binary.
* Build: gcc print-bits.c -o print-bits
* Run: ./print-bits
*/
#include<stdio.h>
#include<stdlib.h>
void printBits(void const * const ptr, size_t size) {
unsigned char *b = (unsigned char*) ptr;
size_t i;
printf("%d bytes:", size);
for (i = 0; i < size; i++) {
printf("%3X", b[i]);
if((i + 1) % 16 == 0)
printf("\n");
}
printf("\n");
}
int main() {
char c = 'þ';
printBits((void*)&c, 2);
short s = 171;
printBits((void*)&s, 2);
short bs = htons(s);
printBits((void*)&bs, 2);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment