How to convert unint_16 to char?

Please have been trying to convert unint_16 to char without any success , could you please help me out ?


uint16_t has two bytes so you can not convert to char directly because char has only one byte.
So you need something like

 uint16_t value = 12345;
 char lo = value & 0xFF;
 char hi = value >> 8;
// or
 char buff[2];
 buff[0] = value & 0xFF;
 buff[1] = value >> 8;
// and so on

BR, Jan

Ooh ok , thank you Sir .
So what will be the result if we print out the variable hi ? And what does variable lo becomes ? We don’t use it anymore ? . Still wondering how the conversion takes place with all these variable ?
Or which one of the variable can be considered as the real converted value of our unint16_t variable ?

what is the intention? Only printf?

BR, Jan

No actually it still have to do with ECG 3 click and GSM-GPS click .
I’m currently trying to send the OutHR ( heartbeat rate ) value which is of data type unint16_t through GSM . Meaning as message , but the message function accept only char data types . So this is the main reason why I’m trying to convert it to char so I can easily send it as SMS text or message using the GSM.


    uint16_t value = 12345;
    char buff[10];
    sprintf(buff, "%d", value);
    printf ("decimal: %s\n",buff);
    #include <string>
    uint16_t value = 12345;
    string temp = to_string(value);
    printf ("decimal: %s\n",temp.c_str());

BR, Jan

Great Sir, please it has worked successfully, I’m really really grateful , thanks a million .