How to get a pointer to the bytes of a uint32_t

Amila Senadheera

I'm trying to create a complete uint32_t using vector of uint8_t bytes. It should be filled iteratively. It can happen in any of the following ways:

  • 1 byte and 3 bytes.
  • 2 bytes and 2 bytes.
  • 4 bytes.
  • 3 bytes and 1 byte.
    uint32_t L = 0;
    uint32_t* LPtr = &L;
    std::vector<uint8_t> data1 = {0x1f, 0x23};
    memcpy(LPtr, data1.data(), 2);
    EXPECT_EQ(0x231f, L);

Above works fine (first two bytes). But following is not (with the two sets of bytes).

    uint32_t L = 0;
    uint32_t* LPtr = &L;
    std::vector<uint8_t> data1 = {0x1f, 0x23};
    std::vector<uint8_t> data2 = {0x3a, 0xee};
    memcpy(LPtr, data1.data(), 2);
    memcpy(LPtr, data2.data(), 2);
    EXPECT_EQ(0x231f, L);
    EXPECT_EQ(0x231fee3a, L);

The issue I feel is LPtr does not point to the next byte that should be filled next. I tried LPtr+2 which is not pointing to individual byte of uint32_t.

This should be done using memcpy and output should go to uint32_t. Any idea to get this sorted?

Endianness is not an issue as of now. It can be corrected once the uint32_t is completed (when eventually 4 bytes get copied).

Any help is appreciated!

Bartosz Charuza

The problem is you're using a pointer to uint32_t so incrementing it won't make it iterate by 1 byte, only by 4 bytes. Here is a version which populates all bytes of L, but it's still messing with endianness:

uint32_t gimmeInteger(std::vector<uint8_t> data1, std::vector<uint8_t> data2)
{
  assert((data1.size() == 2));
  assert((data2.size() == 2));
  uint32_t L = 0;
  uint8_t* LPtr = reinterpret_cast<uint8_t*>(&L);
  memcpy(LPtr, data1.data(), 2);
  memcpy(LPtr+2, data2.data(), 2);
  return L;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to save the uint32_t mask as a pointer variable?

How to get a unique value in Java that corresponds to uint32_t?

How to get pointer offset in bytes?

Variable number of bytes to Uint32_t

How to get pointer offset of an enum member in bytes?

How to convert a string to uint32_t

Understanding uint32_t char typecast (Bytes)

Cast from uint8_t pointer to uint32_t integer compilation error

size_t pointer vs uint32_t pointer in c

How to convert uint32_t number to char[8]?

How to convert uint32_t to unsigned char array?

How to pass uint32_t * as a property to QML

How to convert uint32_t to double or float and print?

How to safely extract a signed field from a uint32_t into a signed number (int or uint32_t)

correct use of malloc in function with passed uint32_t array pointer

Get C FILE pointer from bytes::Bytes in Rust

How to assign min of uint32_t::max and uint64_t in type safe manner?

How to convert uint16_t[2N] to uint32_t[N] effectively?

How to interleave two uint32_t values into one uint64_t?

How to get pointer to pointer in LLVM?

How can a pointer to double that is 4 Bytes point to double that is 8 Bytes?

How to define a type that can hold a uintptr_t or a uint32_t without a union?

How endianness swaps the order of elements, when converting two elements of uint32_t array to a single uint64_t?

How can I create different types from uint32_t that are statically different?

How to print uint32_t variables value via wprintf function?

How do I read uint32_t values out of a nested PROGMEM array?

How can I duplicate an array of pointers in C? (uint32_t)

How to get a pointer of an interface

How to Get ConnectEx() pointer