Cypress Perform

Home > Design Support > Cypress Developer CommunityTM > Cypress Forums > USB Controllers > How to combine four uint8_t to make a uint32_t?

Bookmark and Share
Cypress Developer CommunityTM
Forums | Videos | Blogs | Training | Rewards Program | Community Components



How to combine four uint8_t to make a uint32_t?
Moderator:
RSKV

Post Reply
Follow this topic



How to combine four uint8_t to make a uint32_t?

Nazila posted on 03 Feb 2012 10:20 AM PST
Top Contributor
55 Forum Posts

Hi,

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

Thanks,

Nazila




Re: How to combine four uint8_t to make a uint32_t?

aasi posted on 03 Feb 2012 10:30 AM PST
Cypress Employee
1073 Forum 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.

Cheers,

Anand



Re: How to combine four uint8_t to make a uint32_t?

Nazila posted on 03 Feb 2012 04:38 PM PST
Top Contributor
55 Forum 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.



Re: How to combine four uint8_t to make a uint32_t?

H L posted on 03 Feb 2012 05:55 PM PST
Top Contributor
510 Forum Posts

 unsigned long ulTotal;

 

ulTotal = byte4;

ulTotal = (ulTotal  << 8) + byte3;

ulTotal = (ulTotal  << 8) + byte2;

ulTotal = (ulTotal  << 8) + byte1;



Re: How to combine four uint8_t to make a uint32_t?

Bob Marlowe posted on 04 Feb 2012 12:08 AM PST
Top Contributor
1466 Forum Posts

C has the "union" declaration to solve cases like this. It overlays the same data-area (an uint32) with 4 int8. Look here: http://publications.gbdirect.co.uk/c_book/chapter6/unions.html

union 
{
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!

Bob






ALL CONTENT AND MATERIALS ON THIS SITE ARE PROVIDED "AS IS". CYPRESS SEMICONDUCTOR AND ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY OF THESE MATERIALS FOR ANY PURPOSE AND DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THESE MATERIALS, INCLUDING BUT NOT LIMITED TO, ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT OF ANY THIRD PARTY INTELLECTUAL PROPERTY RIGHT. NO LICENSE, EITHER EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, IS GRANTED BY CYPRESS SEMICONDUCTOR. USE OF THE INFORMATION ON THIS SITE MAY REQUIRE A LICENSE FROM A THIRD PARTY, OR A LICENSE FROM CYPRESS SEMICONDUCTOR.

Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms and Conditions of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms and Conditions of this site. Cypress Semiconductor and its suppliers reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.