I'm using an Ethernet shield and I want to output the uptime of the shield. I'm doing with millis() / 1000 which gives me a long of the uptime in seconds. I need to convert this to a char array so I can send it to the shield. I've been doing everything with sprintf() but sprintf() on avr doesn't appear to handle longs correctly?
unsigned long num = 1099511627776;
char mystr[40];
sprintf(mystr,"Millis: %u",num);
Serial.println(mystr);
I get 0 if I run this code. Other large numbers output either 1 or 2. If I use a standard int it's fine. What's the best way to convert a long to a string if sprintf() won't work?
I'm not printing out the value to serial, I'm sending it to the buffer of my ethernet shield. I'm not sure what the problem with sprintf() on Arduino is, but it doesn't handle unsigned longs properly.
void setup() {
Serial.begin(9600);
}
void loop() {
unsigned long num = 1073741824; // 2^30
char mystr[40];
sprintf(mystr,"Millis: %u",num);
Serial.println(mystr);
delay(1000);
}
That code always outputs 0. Thanks to Emdee for the ltoa() function, that does what I need it to! I'll use that instead.
I hope it is ok to ask another question based on the sprintf topic.
I would like to serial print bytes such as 0x00 through 0xFF and have the printed result to look like 00, FF. Using Serial.print the result would be 0, and FF.
Could some one show a code example of that for me please?
Obviously I'm missing something important, perhaps several things.
How does one use Serial.println() to convert arithmetic values into a formatted character array (or formatted String, if one is of the C++ persuasion)?
How does one force leading zeros on HEX values?
Finally, what is the memory cost of using Serial code when there is no serial communication?
There's memory cost in program memory (flash ram) and some in data memory (sram) when routines put local vars on the stack.
I showed above how to handle leading zeroes. Turning int to char array isn't much harder. To go from 0 to 9 int to 0 to 9 ascii text is add '0' to the int value. To go from 10 to 15 is add 'A'. You will need a loop... horrors!
It is easier to use an elevator than stairs (ie library functions as opposed to write your own) but consider in a very small house how much room the elevator takes. In a PC it is nothing much. In a PC, 100K to say "Hello World" is nothing much. It is only a problem when you -have- to have the elevator because you don't know any better way.
Simple Fact: writing your own will give you greater understanding of the code and machine and the freedom that comes with that.
If you use the code it has a cost. It only matters when you cross limits but it's good practice to avoid waste on a machine with only 2K SRAM. But be however you want and when your practices do get you bugs I am sure you will suddenly understand what to do. Even when you write your own it has a cost.
How does one use Serial.println() to convert arithmetic values into a formatted character array
You can't. Serial.print() does unformatted output.
How does one force leading zeros on HEX values?
Using Serial.print(), you can't. That's why sprintf() is needed.
Finally, what is the memory cost of using Serial code when there is no serial communication?
It's hard to answer this question, since it doesn't make sense.
The thing to keep in mind is that Serial is an instance of the HardwareSerial class, which derives from Stream which derives from Print. The EthernetServer and EthernetClient classes also derive (indirectly) from Print, which is what provides the print() and write() methods.
If you -need- sprintf() then you probably -need- to take a break.
OK. So perhaps need is too strong.
That's why sprintf() is useful.
Yes, there are ways to accomplish all that sprintf() does without using sprintf(). But, sometimes it is just easier to let one line of code do what would take dozens of lines of code to accomplish using just the Print::print() function.
Thanks to you both! I'm just getting up to speed in the Arduino environment, and this thread has been informative and helpful for me. I've written a fairly decent collection of well-exercised C formatting functions (some dating back as far as 1980) so I'm not particularly intimidated by the prospect of rolling my own. Your patience and willingness to provide rationale and detail specific to the Arduino is much appreciated.