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.

Unsigned char to char

Other Parts Discussed in Thread: MSP430FR5739

Hi,

I wrote a function that gets the data from UART and copy to memory, something is wrong here because i am sending test drom COMM operator and getting integer of the string which is sent from pc. Data is being copied to the memory in integer. For example, i am sending the char "B" from pc, MSP430FR5739 copies the data its memory 66 which is ascii of B.

void getUart(char * str)
{
unsigned char getRTCM = 0;
if(UCA0RXBUF) {
getRTCM = UCA0RXBUF;
itoa(getRTCM,str,10);                    //I know itoa gets its integer value, need to convert unsigned char to char?
memcpy(str+strlen(str), " F", strlen(" F"));
}

  • Sorry, I don't really get what you're trying to do here.

    Burak DERYA said:
    if(UCA0RXBUF)

    is not a valid test. UCA0RXBUF always contains something. Even 0x00 migh tbe a valid (binary) value there. And if you received something, it will stay in RXBUF, no matter how often you read it or test it. So if you once received 'x', your funciton will read 'x' every time it is called. As often as you call it.

    To be sure that it is actually a new received byte, you need to check for UCA0RXIFG bit. It is set when a new byte arrived and cleared when you either clear it or read from RXBUF. So you won't work on the same byt ein RXBUF over and over again.

    Instead of using itoa and a rather complex memcpy call to add furhter letters, why don't you use sprintf?

    sprintf(str, "%d F", getRTCM);

    Just to answer your other question: no, it is not necessary to do a conversion. C does an implicit cast to the required number format if necessary. However, for sprintf, the 'required' format is unknown and the compiler pushes what you pass. It will still expand the char parameter to 16 bit. How it is treated (signed/unsigned) depends on the format paramete rint he format string. %d is a decimal (signed int), %u and unsigned int, %c a char, %s a char* to a 0-terminated string etc. All these are 16 bit values (for the char, the upper 8 bits are ignored). signed/unsigned/pointer is just a matter of interpretation.

**Attention** This is a public forum