You are here

How to combine four uint8_t to make a uint32_t? | Cypress Semiconductor

How to combine four uint8_t to make a uint32_t?

Summary: 4 Replies, Latest post by Bob Marlowe on 04 Feb 2012 02:08 AM PST
Verified Answers: 0
Last post
Log in to post new comments.
Nazila's picture
55 posts


It seems very simple but I cannot combine four uint8_t and make uint32_t conventionally or even using the predefined function: "CY_U3P_MAKEDWORD". The result is always the least significant byte:

for example:

uint8_t byte4, byte3, byte2, byte1;

uint32_t readDataD;

byte4 (MSB) = 0xFE; byte3 = 0x56; byte2 = 0x78; byte1 (LSB) = 0x1F;

readDataDW = CY_U3P_MAKEDWORD(byte4, byte3, byte2, byte1);


The result is

readDataDW = 0x1F 0x1F 0x1F 0x1F



aasi's picture
Cypress Employee
1166 posts

I'll look at it and let you know if anything is wrong.

In the meantime you can always shift and add the numbers.



Nazila's picture
55 posts

That method did not work (If you look at the definition of the function is the same idea). Eventually, I used an array and copy them in the memory one by one.

user_460349's picture
1362 posts

 unsigned long ulTotal;


ulTotal = byte4;

ulTotal = (ulTotal  << 8) + byte3;

ulTotal = (ulTotal  << 8) + byte2;

ulTotal = (ulTotal  << 8) + byte1;

user_1377889's picture
9284 posts

C has the "union" declaration to solve cases like this. It overlays the same data-area (an uint32) with 4 int8. Look here:

uint32 Data32;
uint8 Bytes[4];
} My32Var;

My32Var.Bytes[0] = 0xCA;
My32Var.Bytes[1] = 0xFF;
My32Var.Bytes[2] = 0xEE;
My32Var.Bytes[3] = 0x00;


Take care of the endianess of the underlying compiler, Keil and GNU differ here!


Log in to post new comments.