fork(2) download
  1. #include <cstdint>
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include <cstdint>
  6.  
  7. uint32_t read_u32_le(uint8_t* bytes)
  8. {
  9. uint32_t value;
  10. value = bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);
  11. return value;
  12. }
  13. int32_t read_s32_le(uint8_t* bytes)
  14. {
  15. int32_t value;
  16. value = bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);
  17. return value;
  18. }
  19. int main()
  20. {
  21. // written in binary format is the following bytes:
  22. // 68 172 0 0
  23.  
  24. unsigned char b1 = 68;
  25. unsigned char b2 = 127;
  26. unsigned char b3 = 0;
  27. unsigned char b4 = 0;
  28.  
  29. std::cout << (int)b1 << " " << (int)b2 << " " << (int)b3 << " " << (int)b4 << std::endl;
  30.  
  31. uint8_t bytes[4] = {b1, b2, b3, b4};
  32. // prints -21436:
  33. int32_t sampleRate_s = read_s32_le(bytes);
  34. std::cout << sampleRate_s << std::endl;
  35.  
  36. // prints correctly 44100:
  37. uint32_t sampleRate_u = read_u32_le(bytes);
  38. std::cout << sampleRate_u << std::endl;
  39.  
  40. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
68 127 0 0
32580
32580