Question

I have a 8 bytes integer buffer carrying data bytes, and a 32 bytes variable to save these data. How can I swap data bytes to variable except use pointer? (use pointer will case some unknown issue).

Ex.

buffer[0] = 0;
buffer[1] = 1;
buffer[2] = 2;
buffer[3] = 3;

How to shift buffer[0] to variable bit [31:24] , buffer[1] to variable bit [23:16], buffer[2] to variable bit [15:8], buffer[3] to variable bit [7:0].

Was it helpful?

Solution

uint32_t variable;
buffer[0] = 0;
buffer[1] = 1;
buffer[2] = 2;
buffer[3] = 3;
variable = ((buffer[0] << 24)| (buffer[1] << 16) | (buffer[2] << 8) | buffer[3]);

When I used a 32bit varible the data type declearation i used was uint32_t ,please make sure that you use it otherwise use your declarations for uint8_t & uint32_t

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top