#1 wscanf does not read long values correctly
#2 operator precedence or a compiler bug for operatuions like
temp |= value[pos + 1] << 16;
The following code can run on QNX/Sh and also windows.
#include <stdio.h>
#include <wchar.h>
#include <stdint.h>
#include
int main() {
char buff[256];
unsigned long k = 4207677439ul, j;
sprintf(buff, “%lu”, k);
puts(buff);
sscanf(buff, “%lu”, &j);
printf(“J = %lu\n\n”, j);
j=3;
wchar_t Buff[256];
wchar_t Fmt[] = { ‘%’, ‘l’, ‘u’, ‘\0’ };
swprintf(Buff, 256, Fmt, k);
printf("%ls\n", Buff);
swscanf(Buff, Fmt, &j);
printf(“J = %lu\n”, j);
std::vector<uint8_t> value;
value.push_back(0x52);
value.push_back(0x42);
value.push_back(0x52);
value.push_back(0x32);
uint32_t temp = 0, m_value;
unsigned pos = 0;
temp |= value[pos + 3];
temp |= value[pos + 2] << 8;
temp |= value[pos + 1] << 16;
temp |= value[pos] << 24;
printf("\nm_value is %lu\n", m_value);
temp = 0;
temp |= (value[pos + 3]);
temp |= (value[pos + 2] << ;
temp |= (value[pos + 1] << 16);
temp |= (value[pos] << 24);
m_value = temp;
printf("\nm_value is %lu\n", m_value);
}
Result:
4207677439
J = 4207677439
4207677439
J = 4294967295
m_value is 1882540172
m_value is 1380078130