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)
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 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/.
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 ...
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 )
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'.
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.