danielhilst

borl-endian.c

Jul 3rd, 2013
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.61 KB | None | 0 0
  1. /*
  2.  * This program tests the endian of your system
  3.  */
  4. #include <stdio.h>
  5. #include <stdint.h>
  6.  
  7. union byte_order {
  8.     uint32_t dword;
  9.     uint8_t  bytes[4];
  10. };
  11.  
  12. int main(void)
  13. {
  14.     int i;
  15.     union byte_order dw = {
  16.         .dword = 0xAABBCCDD,
  17.     };
  18.    
  19.     printf("Assigned 0x%X\nRepresented by 0x", dw.dword);
  20.     for (i = 0; i < 4; i++) /* 4 is the sizeof(uint32_t) */
  21.         printf("%X", (unsigned char)dw.bytes[i]);
  22.     putchar('\n');
  23.  
  24.     if (dw.bytes[0] == (uint8_t)0xAA)
  25.         puts("Your system is BIG ENDIAN");
  26.     else if (dw.bytes[0] == (uint8_t)0xDD)
  27.         puts("Your system is LITTLE ENDIAN");
  28.     else
  29.         puts("I can't tell what your system is...");
  30.  
  31.     return 0;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment