fork download
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4. struct stRequestMsg {
  5. uint16_t startTag;
  6. uint32_t messageSize;
  7. uint16_t messageID;
  8. uint16_t sequenceNumber;
  9. uint16_t messageType;
  10. uint32_t reserved;
  11. };
  12.  
  13. void printIt(struct stRequestMsg m) {
  14. printf("{\n"
  15. " .startTag = %#x;\n"
  16. " .messageSize = %#x;\n"
  17. " .messageID = %#x;\n"
  18. " .sequenceNumber = %#x;\n"
  19. " .messageType = %#x;\n"
  20. " .reserved = %#x;\n"
  21. "}\n",
  22. m.startTag, m.messageSize, m.messageID,
  23. m.sequenceNumber, m.messageType, m.reserved);
  24. }
  25.  
  26.  
  27. uint16_t deserialize_uint16(char const * const b) {
  28. return ((uint16_t) b[0] << 8u) |
  29. ((uint16_t) b[1]);
  30. }
  31. uint32_t deserialize_uint32(char const * const b) {
  32. return ((uint16_t) b[0] << 24u) |
  33. ((uint16_t) b[1] << 16u) |
  34. ((uint16_t) b[2] << 8u) |
  35. ((uint16_t) b[3]);
  36. }
  37.  
  38. struct stRequestMsg deserialize(char const * const b) {
  39. struct stRequestMsg r;
  40. r.startTag = deserialize_uint16(b);
  41. r.messageSize = deserialize_uint32(b + 2);
  42. r.messageID = deserialize_uint16(b + 6);
  43. r.sequenceNumber = deserialize_uint16(b + 8);
  44. r.messageType = deserialize_uint16(b + 10);
  45. r.reserved = deserialize_uint32(b + 12);
  46. return r;
  47. }
  48.  
  49. int main(void) {
  50. char buff[16];
  51. buff[0] = 0x50;
  52. buff[1] = 0x00;
  53. buff[2] = 0x1E;
  54. buff[3] = 0x00;
  55. buff[4] = 0x00;
  56. buff[5] = 0x00;
  57. buff[6] = 0x31;
  58. buff[7] = 0x00;
  59. buff[8] = 0x10;
  60. buff[9] = 0x00;
  61. buff[10] = 0x20;
  62. buff[11] = 0x00;
  63. buff[12] = 0x10;
  64. buff[13] = 0x00;
  65. buff[14] = 0x00;
  66. buff[15] = 0x00;
  67.  
  68. struct stRequestMsg msg = deserialize(&(buff[0]));
  69. printIt(msg);
  70. return 0;
  71. }
  72.  
Success #stdin #stdout 0s 2156KB
stdin
Standard input is empty
stdout
{
  .startTag =       0x5000;
  .messageSize =    0x1e000000;
  .messageID =      0x3100;
  .sequenceNumber = 0x1000;
  .messageType =    0x2000;
  .reserved =       0x10000000;
}