Search code examples
cstm32cortex-m

Issues with converting int to char in C


I've wrote a small function for debug purpose which sends string via USB to a terminal :

void localprint(char* msg){
if(strlen(msg) > 60){
            usb_sendData(msg,60);
}
else usb_sendData(msg,strlen(msg));

} the function works fine for strings like :

localprint(" I'm a test" ), 

the message is displayed on terminal. Now I want to get the adcValue which is an int on the terminal, for I've tried :

char t= (char)(((int)'0')+getADCValue(9)); // getting the ADC value of 0th channel 
localprint(&t);

this doens't work at all , I'Ve also tried :

char t= (char) getADCVAlue(9);
localprint(&t);

it doesn't work neither. So my question is any idea how can I do that . I'm using uC STM32f10xx and ARM gcc. thanks for any hint


Solution

  • Without knowing anything about the range of possible ints, something like this will work:

    char buffer[32];
    sprintf(buffer, "%d", getADCVAlue(9));
    localprint(buffer);
    

    Since you did 0+, I'm assuming it's one digit, so you could go

    char buffer[2];
    buffer[0] = '0' + getADCVAlue(9);
    buffer[1] = '\0';
    localprint(buffer);