Reputation: 147
I need to convert bytes into an int (or uint8/16/32) in C. Here is a explanation of my code :
char* out;
//init
//out=read_udp (from equipment)
for(int i=0; i<n; ++i)
printf("out[%d]=%x ", i, out[i]);
Output:
out[0]=0x00 out[1]=0xAA out[2]=0x44 out[3]=0x12 out[4]=0x2B out[5]=0x00 out[6]=0x7E out[7]=0x3B
The first four bytes are a header, followed by an ID and a value (two little-endian bytes each). How can I get the ID and value?
I tried making pointers to the integers:
char* p_ID;
char* p_val;
//malloc
p_ID=&out[4];
p_val=&out[6];
printf("%d %d", p_ID, p_val);
I also tried changing the byte order with
p_ID[0]=out[1]; p_ID[1]=out[2];
I also tried sscanf and strol solutions but I get weird results.
Upvotes: 1
Views: 139
Reputation: 144
Let me provide an example which you're looking for.
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#define SWAP2BYTES(val) (((val>>8)&0x00FF)|((val<<8)&0xFF00))
bool isLittleEndian() { // just to check host endian
uint32_t val = 1;
uint8_t *c = (uint8_t*)&val;
return (1 == (uint32_t)*c);
}
// if the same endian, then no need to swap, otherwise swap
#define CHECKSWAP(val) (isLittleEndian()?val:SWAP2BYTES(val))
int main(int argc, char **argv) {
uint8_t out[8] = {0x00,0xAA,0x44,0x12,0x2B,0x00,0x7E,0x3B};
uint16_t id, val;
// to prevent from alignment issue, memcpy used insted of assignment
memcpy((uint8_t*)&id, &out[4], sizeof(id));
memcpy((uint8_t*)&val, &out[6], sizeof(val));
id = CHECKSWAP(id);
val = CHECKSWAP(val);
printf("outdata = ");
for (int i = 0; i < sizeof(out); i++)
printf("0x%02x ", out[i]);
printf("\n");
printf("Header: 0x%02x 0x%02x 0x%02x 0x%02x.\n", out[0], out[1], out[2], out[3]);
// just to make sure whether or not intended values are retrieved
printf("ID: %d(0x%04x), Value: %d(0x%04x).\n", id, id, val, val);
return 0;
}
On my pc, the below is the output:
outdata = 0x00 0xaa 0x44 0x12 0x2b 0x00 0x7e 0x3b
Header: 0x00 0xaa 0x44 0x12.
ID: 43(0x002b), Value: 15230(0x3b7e).
Upvotes: 1