cancel
Showing results for 
Search instead for 
Did you mean: 

trying to split 16bit number into two 8 bit number uint8_t bytes[2]; uint16_t value = 1234; bytes[0]= value >> 8; bytes[1]= value & 0xFF; what is the wrong in this code . cube ide keep giving compile error .

MMARI.1
Senior
 
6 REPLIES 6
alister
Lead

The error is likely to be in the context. If you could post the code and the error, it'll save guessing, and most likely, you'll also get some suggestions, e.g. about unions, that'd save code and cycles.

alister
Lead

#include <stdint.h>

MMARI.1
Senior

Great Alister .... thanks ... i already included stdio.h stdlib.h & maths.h its's started working . i think stdint.h inclusion also additional help .

MMARI.1
Senior
uint16_t number11=0;
 
union
{
uint8_t  bytes[2];
uint16_t number1;
}memory16bits;
 
union
{
    uint16_t value1;
 
    struct
    {
        uint8_t byt[2];
 
 
    }bytnumber;
 
}pixel;
int main(void ) {
 while (1)
  {
    /* USER CODE END WHILE */
	  pixel.value1 =12345;
 
	 memory16bits.bytes[0]=pixel.bytnumber.byt[0];
	 memory16bits.bytes[1]=pixel.bytnumber.byt[1];
 
 
	  number11 = memory16bits.number1;
 
 
	  HAL_Delay(1000);
	  pixel.value1 =55555;
	  memory16bits.bytes[0]=pixel.bytnumber.byt[0];
	  memory16bits.bytes[1]=pixel.bytnumber.byt[1];
	  number11 = memory16bits.number1;
 
 
 
	  HAL_Delay(1000);
 
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}
 
}

Working perfect splitting 16 bit into two 8 bit and recombine two 8 bit into single 16 bit .

how to split four letter " STAT "or " STOP" string into single char and recombine using union and struct ?

Piranha
Chief II

There is no string type in C. A string is just an array of char elements terminated with a zero.

https://en.cppreference.com/w/c/language/string_literal