0
I have a function in C that takes two bytes one with higher significant value (msb) and the other with lower value (lsb) and then converts them to decimal.
int get_pea(char *buffer)
{
char lsb[8], msb[8], pea[16];
unsigned int i;
convertDecimalBinary((int) buffer[0], lsb, 8);
convertDecimalBinary((int) buffer[1], msb, 8);
for (i=0; i<8; i++) {
pea[i] = msb[i];
}
for (i=0; i<8; i++) {
pea[i+8] = lsb[i];
}
return convertBinaryDecimal(pea, 16);
}
This function is pretty dumb with so many type conversions, I know that in C there is no need to do so much, but I saw no other way to:
buffer[0] = 0x84;
buffer[1] = 0x03;
Having these two bytes as I convert to decimal the 0x03 0x84 ?
Just one question, why
pea
? Any relation to peas?– Jefferson Quesado
0x03 is a number. If you only need the decimal representation just use the
%d
of the function familyprintf
. The same for 0x84. Being in hexa, binary or decimal is only form of representation. The number in its essence exists by itself, it does not need anything else. If you want to join the two bytes considering thatbuffer[0]
is the low-byte is that thebuffer[1]
is the high-byte of a little-endian 16-bit word, it would suffice to do the high-byte bitwise shift in 8 houses and join with low-byte:(buffer[0] & 0xff) | ((buffer[1] & 0xff) >> 8)
– Jefferson Quesado