#include <cstdint>
#include <iostream>
#include <string>
#include <vector>
#include <cstdint>
uint32_t read_u32_le(uint8_t* bytes)
{
uint32_t value;
value = bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);
return value;
}
int32_t read_s32_le(uint8_t* bytes)
{
int32_t value;
value = bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);
return value;
}
int main()
{
// written in binary format is the following bytes:
// 68 172 0 0
unsigned char b1 = 68;
unsigned char b2 = 127;
unsigned char b3 = 0;
unsigned char b4 = 0;
std::cout << (int)b1 << " " << (int)b2 << " " << (int)b3 << " " << (int)b4 << std::endl;
uint8_t bytes[4] = {b1, b2, b3, b4};
// prints -21436:
int32_t sampleRate_s = read_s32_le(bytes);
std::cout << sampleRate_s << std::endl;
// prints correctly 44100:
uint32_t sampleRate_u = read_u32_le(bytes);
std::cout << sampleRate_u << std::endl;
}
I2luY2x1ZGUgPGNzdGRpbnQ+CiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGNzdGRpbnQ+Cgp1aW50MzJfdCByZWFkX3UzMl9sZSh1aW50OF90KiBieXRlcykKewogICAgdWludDMyX3QgdmFsdWU7CiAgICB2YWx1ZSA9IGJ5dGVzWzBdIHwgKGJ5dGVzWzFdIDw8IDgpIHwgKGJ5dGVzWzJdIDw8IDE2KSB8IChieXRlc1szXSA8PCAyNCk7CiAgICByZXR1cm4gdmFsdWU7IAp9CmludDMyX3QgcmVhZF9zMzJfbGUodWludDhfdCogYnl0ZXMpCnsKICAgIGludDMyX3QgdmFsdWU7CiAgICB2YWx1ZSA9IGJ5dGVzWzBdIHwgKGJ5dGVzWzFdIDw8IDgpIHwgKGJ5dGVzWzJdIDw8IDE2KSB8IChieXRlc1szXSA8PCAyNCk7CiAgICByZXR1cm4gdmFsdWU7IAp9CmludCBtYWluKCkKewogICAgLy8gd3JpdHRlb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