fork download
  1. #include <stdio.h>
  2. #include <limits.h>
  3.  
  4. #if CHAR_BIT != 8
  5. #error char is expected to be 8 bits
  6. #endif
  7.  
  8. unsigned char RevByte(unsigned char b)
  9. {
  10. static const unsigned char t[16] =
  11. {
  12. 0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
  13. 0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF
  14. };
  15. return t[(b >> 4)] | (t[b&0xF] << 4);
  16. }
  17.  
  18. void RevBytes(unsigned char* b, size_t c)
  19. {
  20. size_t i;
  21. for (i = 0; i < c / 2; i++)
  22. {
  23. unsigned char t = b[i];
  24. b[i] = RevByte(b[c - 1 - i]);
  25. b[c - 1 - i] = RevByte(t);
  26. }
  27. if (c & 1)
  28. b[c / 2] = RevByte(b[c / 2]);
  29. }
  30.  
  31. int main(void)
  32. {
  33. int i;
  34. unsigned char buf[16] =
  35. {
  36. 0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
  37. 0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF
  38. };
  39.  
  40. RevBytes(buf, 16);
  41.  
  42. for (i = 0; i < 16; i++)
  43. printf("0x%02X ", buf[i]);
  44. puts("");
  45.  
  46. return 0;
  47. }
  48.  
Success #stdin #stdout 0s 1788KB
stdin
Standard input is empty
stdout
0xF0 0xE0 0xD0 0xC0 0xB0 0xA0 0x90 0x80 0x70 0x60 0x50 0x40 0x30 0x20 0x10 0x00