This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Conversion of unsigned int to unsigned char

Other Parts Discussed in Thread: CC430F5137, CC1101

Hello every one,

I am using cc430f5137, i am getting value from ADC12 which is unsigned int, now i want to transfer it through cc1101 module for which i need unsigned char value. In an ADC ISR i copied ADC12MEM2 value to num variable and then by using the memcpy command i want to convert it into char. when i try to look ch in Expression box it shows only "." can any one know how to convert it.

  • The "." looks like it is shown as ASCII. In the expressions box, right click on the variable -> Number format and select the desired format.
    But what do you want to have as value? Do you want chars? This would mean you will have to split your 12-bit value of the ADC12 into four seperate ASCII characters. 1012 would lead to '1' '0' '1' '2'. Or do you want to reduce the 12-bit value to an 8-bit one?

  • Since unsigned int is of 16 bits and unsigned char is of 8 bits, so i want to see converted int value into character value i.e.
    if i an integer = 0xfedc
    then ch an char shows like ch[0] = 0xfe ch[1] = 0xdc
    i checked the number format and on by clicking the String option it only shows ")" and nothing else.
    What actually i want to say that i need 8 bits to transfer so for that i need to convert int into char, that's all.
  • String format is ASCII - you want hex.

    Anyway - you want to split your 16 bits into two 8 bits - this is done by

    uint16_t adc_value = 1012; // In HEX: 0x03F4
    
    uint8_t high_byte, low_byte;
    
    high_byte = (adc_value >> 8);
    low_byte  = adc_value;

  • May be the procedure itoa is needed.
  • old_cow_yellow said:
    May be the procedure itoa is needed.

    I don't think so because

    Waqas Ali Khan said:
    if i an integer = 0xfedc
    then ch an char shows like ch[0] = 0xfe ch[1] = 0xdc

    looks like if he just want to split his 16 bits into two 8 bits.

  • An efficient way (no extra code needed) to Read/Write a Byte, From/To an Integer, is to use a 'union'.

    typedef struct HighLowByte {
    	unsigned char	Low;
    	unsigned char	High;
    } HighLowByte;
    
    typedef union UnsignedIntegerByte {
    	unsigned int	Integer;
    	HighLowByte		Byte;
    	unsigned char	Array[2];
    } UnsignedIntegerByte;
    
    UnsignedIntegerByte	ADCvalue;
    unsigned char	test;
    
    int main(void) {
    	WDTCTL = WDTPW + WDTHOLD; // hold WDT
    
    	ADCvalue.Integer = 0x1234;
    	test = ADCvalue.Byte.Low;
    	test = ADCvalue.Array[0];	// same value as Low-byte
    	test = ADCvalue.Byte.High;
    	test = ADCvalue.Array[1];	// same value as High-byte
    
    }
    

  • Interesting - never used it that way. Thanks for sharing!
  • itoa is not found in stdlib.h file, can you please mention it in which header file it is found.
  • If you don't need typedef (though typedef are great to make it a easy extern globaly)

    volatile union
    { unsigned int adcvalue;  
      struct{
        char low;
        char high;
      }adc_byte;
    };
    
    adcvalue = ADC12MEM;
    UCB0TXBUF = adc_byte.high;
    UCB0TXBUF = adc_byte.low;

    But if you like typedef place this in common.h

    typedef struct myintTag{
      char low;
      char high;
    } myintstruct;
    
    typedef union int_bytes { 
     unsigned int Integer;
     myintstruct Byte;  
    }int_bytes;
    
    extern int_bytes ADC12; // just one now, but this typedef can be reused over and over.

    and in main you can do this:

    #include 	"msp430.h"
    #include 	"common.h"
    int_bytes ADC12;
    
    int main( void )
    {...
      ADC12.Integer = ADC12MEM;
      UCB0TXBUF = ADC12.Byte.low;
      UCB0TXBUF = ADC12.Byte.high;

**Attention** This is a public forum