How to print out char in regular string format

Hi guys

So, I have bought the as7262 sensor from SparkFun and built a code around it.

I'm unfortunately experiencing that sometimes, the method below doesn't return anything (it seems fairly random when it decides not to return anything):

char* returnMeasurements()
{

    char *NIRString;

    sensor.begin(Wire, GAIN, MEASUREMENT_MODE);
    sensor.takeMeasurementsWithBulb();
    NIRString = "610nm: " + char(sensor.getCalibratedR()) + char("\n680nm: ") + char(sensor.getCalibratedS()) + char("\n730nm: ") + char(sensor.getCalibratedT()) + char("\n760nm: ") + char(sensor.getCalibratedU()) + char("\n810nm: ") + char(sensor.getCalibratedV()) + char("\n860nm: ") + char(sensor.getCalibratedW()) + char("\n");
    Serial.print(NIRString);
    return  NIRString;
}

My conclusion is, that since the string is very long I should try to make it occupy less of the ram on my Uno. Which is what I have tried by converting it into a char string.

Unfortunately, when I try to print it out, I get a lot of weird symbols. Since I know, that this isn't related to a baud rate issue, I suppose that my way of utilizing less space for my string is wrong.

Any help is much appreciated :-)

That would be because you're returning a pointer that ceases to exist after returnMeasurements() exits. You also can't concatenate char arrays that way.

You also didn't bother to post your full code or tells us what the 'sensor' object is.

char *NIRString;

Doesn't point to memory that you own, so you CANNOT dereference it.

NIRString = "610nm: " +

You CANNOT add strings like this. Happily, if all you need to do is print it, you don't need to.

gfvalvo: You also didn't bother to post your full code or tells us what the 'sensor' object is.

Why would that be necessary?

AWOL: char *NIRString;

Doesn't point to memory that you own, so you CANNOT dereference it.

NIRString = "610nm: " +

You CANNOT add strings like this. Happily, if all you need to do is print it, you don't need to.

Thank you for your reply. What do you suggest I do then? If I use a regular string object, there isn't enough memory, and my data will be lost.

What do you suggest I do then?

That depends on what it is you're trying to do, which is impossible to say, from the snippet you posted.

Avacay: Why would that be necessary?

Here's one reason:

AWOL: That depends on what it is you're trying to do, which is impossible to say, from the snippet you posted.

Basically, the return in my method is being sent to a mobile application. I could send any format to my mobile application, currently, I do the following:

BTSerial.print(returnMeasurements);

The above method sends a byte string that I convert in my app code

The above method sends a byte string that I convert in my app code

Call it multiple times, once for each individual phrase of the string.