Best way to convert byte to String?

I spent too much time the other day trying to convert a number held as a byte to characters in a String. The result was code like below. This is not compact nor elegant ad I'd really like to know how it should be done.

(RTCmonth is shown fixed here for simplicity, but it's variable in the code. A .CSV file is created with a timestamp as filename)

byte RTCmonth = 12;

String logfilename = "00000000.csv"; // MMDDHHmm.csv

void setlogfilename() { switch (RTCmonth/10) { case 1: logfilename.setCharAt(0,'1'); break; case 2: logfilename.setCharAt(0,'2'); break; case 3: logfilename.setCharAt(0,'3'); break; case 4: logfilename.setCharAt(0,'4'); break; case 5: logfilename.setCharAt(0,'5'); break; case 6: logfilename.setCharAt(0,'6'); break; case 7: logfilename.setCharAt(0,'7'); break; case 8: logfilename.setCharAt(0,'8'); break; case 9: logfilename.setCharAt(0,'9'); break;}

switch (RTCmonth - (RTCmonth/10)*10) { case 1: logfilename.setCharAt(1,'1'); break; case 2: logfilename.setCharAt(1,'2'); break; case 3: logfilename.setCharAt(1,'3'); break; case 4: logfilename.setCharAt(1,'4'); break; case 5: logfilename.setCharAt(1,'5'); break; case 6: logfilename.setCharAt(1,'6'); break; case 7: logfilename.setCharAt(1,'7'); break; case 8: logfilename.setCharAt(1,'8'); break; case 9: logfilename.setCharAt(1,'9'); break;}

I think that sprintf() is what you need.

I advise against using the String class in all but the simplest sketches, because of the amount of RAM is can get through and the fragmentation that it causes. You can use the 'itoa' function to convert the number to a null-terminated array of characters. See http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/.

dc42: You can use the 'itoa' function to convert the number to a null-terminated array of characters. See http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/.

Here's the AVR doc for itoa: http://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html.

Note that 'itoa' is not a standard ANSI/ISO C or C++ function.

PeterH: I think that sprintf() is what you need.

'sprintf' is a standard and pretty powerful, but will most likely use more (program) memory on your Arduino than the way simpler 'itoa'.

You can also look into PString, which uses the Print facility of the Arduino libraries: http://arduiniana.org/libraries/PString

switch (RTCmonth - (RTCmonth/10)*10) {
    case 1:  logfilename.setCharAt(1,'1'); break;

What about October?

Edit: Forget that - default '0' in string

If you are only converting single digits as in your code, you can do this:

// Argument digit must be 0 to 9
static char digitToAscii (uint8_t digit)
{
    return digit + '0';
}

The "static" keyword will only make the generated code smaller.

thanks for the various suggestions. Ironically I was trying to get rid of PString and move to using the new String object when I came across this limitation. seems like something String should do... darn.

I'm using a Mega 1280 so memory usage is not a concern (well, yet)

I like the simplicity of digitToAscii and will try that in this case, but I'm really looking for a method to convert a value in one elegant stroke and not be bothered by handling units, then tens etc. So iota seems best option in future ...

Njay:

// Argument digit must be 0 to 9

static char digitToAscii (uint8_t digit)
{
    return digit + ‘0’;
}




The "static" keyword will only make the generated code smaller.

I assume unit8_t means unsigned integer 8 bits or something like that, but for my edification is there a good reference listing all these types?

also ‘static’ here seems to replace ‘void’. How may other “openers” are there ? (my shallow s/w background must be showing :blush:)

also 'static' here seems to replace 'void'.

No, "char" has replaced "void" as the return type. "void" says the function doesn't return anything.

The memory saving of using the "static" qualifier is minimal.

ninja2:
I assume unit8_t means unsigned integer 8 bits or something like that, but for my edification is there a good reference listing all these types?

Typedefs such as uint8_t are commonly used in embedded software, because the range of a plain type like “int” depends on the processor. For the Arduino 0022 software under Windows, you will find their definitions in C:\arduino-0022\hardware\tools\avr\avr\include\stdint.h.

ninja2:
also ‘static’ here seems to replace ‘void’. How may other “openers” are there ? (my shallow s/w background must be showing :blush:)

No, ‘void’ would be the return type of the function (i.e. returning nothing), but the return type is declared as ‘char’ in this case. Adding ‘static’ here means the function is only available within this source file. This in turn allows the compiler to not generate the function at all if instead it inlines all calls to it. ‘static’ is a storage class specifier; other storage classes specifiers are ‘auto’ (never used in practice), ‘register’ (ditto) and ‘extern’.

excellent thanks dc42

AWOL: The memory saving of using the "static" qualifier is minimal.

"minimal" is quite relative ;). Depends on your available space, on how many times you call the function and on how many functions you have. I have and have seen a few projects that are only possible with this. Same keyword applies to global variables. Some people just squeeze it, some people just buy hw.

The datatypes can be consulted in the WinAVR documentation (lib C reference), I think it's bundled with the Arduino IDE.

Njay:
The datatypes can be consulted in the WinAVR documentation (lib C reference), I think it’s bundled with the Arduino IDE.

Or here: avr-libc: <stdint.h>: Standard Integer Types