Created
November 30, 2016 09:51
-
-
Save ictlyh/5f9b435b292dfa526be611f34d8cd5a9 to your computer and use it in GitHub Desktop.
Demo of print bytes in binary
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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