How can I convert uint16_t into char

Dear All,

I have some problem to save a unint16_t calue into char.

Here is my function to get the Volt value
(Consider logToSd is egal to 2. It will display in Serial Monitor and write to the SD)
It correctly display in Serial Monitor, but it write bad caracter

Here is my first fonction with comments

// logToSd is egal to 2
void getBatt(int logToSd){
  uint16_t vbat;
// Have attention to &vbat
  if (! fona.getBattVoltage(&vbat)) {
    //sprintln(F("\nBatt Volt Fail"),logToSd); 
  }
  else
  {
// Here it display in Serial monitor and write to SD, See below
// It the second sprint() which cause the issue : sprint(vbat,logToSd)
    sprint(F("nVBat:\t"),logToSd); sprint(vbat,logToSd); sprintln(F(" mV"),logToSd); 
  }


  if (! fona.getBattPercent(&vbat)) {
    sprintln(F("Batt % Fail"),logToSd);
  }
  else
  {
//    sprint(F("VPct:\t"),logToSd); sprint(vbat,logToSd); sprintln(F("%"),logToSd);
  }
}

Now my problem is in that function with my comment.
As vbat is a uint16_t

void sprint(uint16_t message, int logToSd)
{
  if(logToSd == 0 || logToSd == 2)
  { 
// Here is deiplay the correct value
    SerialUSB.print(message);
  }
  
  if(logToSd == 1 || logToSd == 2)
  {
// Here is display a strong value (continue reading next code, below
      sd_write(sd_logFile,(char *)&message);
  }
}

(sd_logFile is define as a char (logfile.txt). This works fine)

We have to convert uint16_t to char, because sd_write only accpet char.

Here is my next function

void sd_write(char * fileName, char * text)
{
  // As it's sd_write, the is no \n or \r, so the last parameter is false
  sd_write(fileName, text, false);
}

And now the sd_write function where the bad caracters is displayed

void sd_write(char * fileName, char * text, bool ln)
{
  #ifdef LOGGER
    if(isSdReady == true)
    {
      digitalWrite(PIN_SDLED, HIGH);
      
        logfile = SD.open(fileName, FILE_WRITE);

        // if the file opened okay, write to it:
        if (logfile) 
        {
          sprint(F("Writing ["),0);
          sprint(text,0);    // HERE IS DISPLAY THE BAD CARACTER
          sprint(F("] to "),0);
          sprint(F(fileName),0);
          if(ln == true)
          {
            logfile.println(text);
          }
          else
          {
            logfile.print(text);
          }
          logfile.flush();
          // close the file:
          logfile.close();
          sprintln(F(" => Done."),0);
        }
        else
        {
          // if the file didn't open, print an error:
          sprint(F("\nError opening "),0); sprintln(fileName,0);
        }
        
      digitalWrite(PIN_SDLED, LOW);

      // delay(100);
    }
  #endif
}

And here is the display :

4004
Writing [�D] to ADALOG39.TXT => Done.

Instead of

�D

I should have

4004

I think is a convertion problem from uint16_t to char, and I am saking my self if is due to the & here

if (! fona.getBattVoltage(&vbat))

I hope you could follow my "path" and can help me to correctly save the uint16_t value into my SD file, in char.

Many thnak

sprintln(F("\nBatt Volt Fail"),logToSd);

What's that?

Sorry, I wrote my comment in the bad line.
(This line can be commented...)

This is display when

if(! fona.getBattVoltage(&vbat))

return false. But itt return true.
I corrected my post

After 3k/4 posts, don't you think it's about time you post all your code?

AWOL:
After 3k/4 posts, don't you think it's about time you post all your code?

And get it clear in your head that you can NOT convert anything larger that a byte to a char. It is possible to convert the uint16_t to a string, but it is NOT possible to convert it to a char.

And, in general, there is no reason to convert it. Let the function that is supposed to deal with it deal with converting it. The Serial.print() method knows how to do the conversion. The File.print() method knows how to do the conversion.