uint32_t to bytes

Hi Guys,
Can anyone help me with this? i want to convert uint32_t to bytes in C++.

const uint32_t mydata = 0x00000FF1A; please help
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <iomanip>
#include <cstdint>

union mix_t
{
    std::uint32_t theDWord;
    std::uint8_t theBytes[4];
};

int main()
{
    mix_t aVal;
    aVal.theDWord = 0x0000FF1A;

    std::cout << std::hex << aVal.theDWord << std::endl;

    for (int i = 0; i < 4; ++i)
    {
        std::cout << "Byte #" << i << ": ";
        std::cout << std::hex << (int)aVal.theBytes[i] << std::endl;
    }

    return 0;
}
ff1a
Byte #0: 1a
Byte #1: ff
Byte #2: 0
Byte #3: 0
Last edited on
thank u soo much..May God bless you
Note that the order in which the above program prints the bytes depends on the endianness (byte order) used by your computer. https://en.wikipedia.org/wiki/Endianness
That's quite clever: I didn't realise one could use a union like that :+)

I mean the value is stored with one type, but you use the other type to retrieve it .....

So that must mean that there is no check on whether the type requested via the access is the same as the type on storage. Obviously one has to have the right types / sizes to give a coherent result.

++ThingsLearnt;
I found this on cppreference:
http://en.cppreference.com/w/cpp/language/union

The union is only as big as necessary to hold its largest data member. The other data members are allocated in the same bytes as part of that largest member. The details of that allocation are implementation-defined, and it's undefined behavior to read from the member of the union that wasn't most recently written. Many compilers implement, as a non-standard language extension, the ability to read inactive members of a union.


The example shows some similar to what booradley60 had.

But then, further down:

If two union members are standard-layout types, it's well-defined to examine their common subsequence on any compiler.
Actually iam trying to read card from a RFID reader, i want my reader to read data from a port.i have opened com port 1.

1.3 Reader answer info(tag indunction):
Answer info when tag induction (DATA):<00000FF1A >

SOH TYPE ID FC DATA BCC (8 BITS BCC) END


0x0A A 1 F 00000FF1A BCC1 BCC2 0x0D


//////////////////////////////////////////////////////////

to write data to a port i think it should be in byte format.so i need to convert uint32_t value 00000FF1A to a byte
According to TheIdeasMan, the example from booradley60 is non-standard behavior.

Another way to read the bytes of an int is using bitwise operators
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main()
{
    unsigned int input = 0x392F1234;
    
    unsigned int a = input & 0xFF;
    unsigned int b = (input >> 8) & 0xFF;
    unsigned int c = (input >> 16) & 0xFF;
    unsigned int d = (input >> 24) & 0xFF;
    
    std::cout << std::hex << a  << '\n' << b << '\n' << c << '\n' << d << '\n';
}


Or you could cast the int to a bunch of char:
1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main()
{
    unsigned int input = 0x392f1234;
    unsigned char *bytes = reinterpret_cast<unsigned char*>(&input);
    
    std::cout << std::hex << static_cast<int>(bytes[0])  << '\n' << static_cast<int>(bytes[1]) << '\n' 
            << static_cast<int>(bytes[2]) << '\n' << static_cast<int>(bytes[3]) << '\n';
}
The order of bytes always matters; it is rarely onerous to use bit operators to get the pieces of the integer you need.
Topic archived. No new replies allowed.