Convert a long to a string?

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?

When I try to compile that code:

integer constant is too large for 'long' type

A long on the Arduino is a 32-bit integer.

Pete

I believe you want the ltoa() function. Usage would be something like this:

  char buf[16];
  long lval;

// assign lval something then...
  ltoa(lval,buf,10);

  Serial.println(buf);

Try:

sprintf(mystr,"Millis: %lu",num);

sprintf() will handle it as well as you tell it to. :)

Why make life or code harder than it already is?

unsigned long num = 123456; Serial.println(num);

Honest, it transmits "123456".

I agree. OP questioned sprintf() ability to properly convert a long into a formatted character array. :)

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.

Try using %ul instead of %u

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?

unsigned char c = 0xBE;
char buffer[80];

sprintf(buffer, "c = %2.2X :-)", c);

Thank you I will test it when I get home!

You can do that without sprintf and maybe some day check the difference in code size when using sprintf.

Code would look like (as in untested) this:

byte x = 0xc;

if ( x < 0x10 ) Serial.printf( "0" );
Serial.print( x, HEX );

@GoForSmoke - You're solving the wrong problem. Quoting Scott, the OP:

I'm not printing out the value to serial, I'm sending it to the buffer of my ethernet shield.

He's using Serial.print() to send a string of characters that are the text equivalent to a numeric value.

It doesn't matter where the text is going. That is what I was solving.

You guys are trying to make the very simple into something much less simple.

Obviously I'm missing something important, perhaps several things.

1) 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)?

2) How does one force leading zeros on HEX values?

3) 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.

PaulS:

How does one force leading zeros on HEX values?

Using Serial.print(), you can't. That's why sprintf() is needed.

Check reply 11 on page 1.

If you -need- sprintf() then you probably -need- to take a break. In this case it's like calling a fire truck just to put out a fire in a frying pan.

If you -need- sprintf() then you probably -need- to take a break.

OK. So perhaps need is too strong.

That's why sprintf() is [u]useful[/u].

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.