Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

166
Vistas
Copy larger size variable unit16 to equivalent smaller array size unint8 by casting in c

I have variable uint16 value, I want to copy it to uint8_t buffer[3]. Is it possible to do (Little endian):

*buffer=*(uint8_t *)&value;

Instead of:

buffer[0] = highByte(value);
buffer[1] = lowByte(value);

Since this replacement cause stm32f7 I2C to not working correctly. Is there any correct casting?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

STM32 is little endian so you get the lowest significant byte first:

uint8_t* ptr = (uint8_t*)&value;
uint8_t low  = ptr[0];
uint8_t high = ptr[1];

Doing casts and de-referencing like this is fine for character types only. The above code is assuming that uint8_t is a character type, which is very likely the case (on gcc and other mainstream compilers).

For more info see What is CPU endianness?

EDIT:

If you simply wish to copy a 16 bit number into an array of bytes, the correct solution is this:

memcpy(buffer, &value, sizeof(uint16_t)).

We cannot do *(uint16_t*) buffer=value; because it invokes undefined behavior. buffer could be misaligned and it's also a strict aliasing violation. And this is why I wrote with emphasis above "this is fine for character types only".

over 4 years ago · Santiago Trujillo Denunciar

0

Is there any correct casting?

No

*buffer = <expression>

will always and only write to buffer[0] and never to buffer[1].

One thing you could do - but I strongly urge you NOT to is stuff like this:

uint16_t *ptr = (uint16_t*)buffer; // Ugh, danger
*ptr = value;

This will be problematic if you run it on machines with different endianess. And it's quite possible that there are other reasons to not do this too. As soon as you ask yourself if you can solve something via casting, you should really take a step back. Here are two answers I have written about that:

https://stackoverflow.com/a/62563330/6699433

https://stackoverflow.com/a/63773195/6699433

over 4 years ago · Santiago Trujillo Denunciar

0

Assuming to resolve the problem related to high side and low side as mentioned by @Lundin, and The fact that dereferencing uint8_t grant access to only it's first array element, I reached this solution by only a single cast:

*(uint16_t*) buffer=value;

Which is reduced version of:

uint16_t* p;
p= buffer;
*p=value;
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda