String, convert end to long

Hello,

My searching has run afoul. The short version of what I would like help with is taking part of a string (substing) and convert it to a long. The incoming string has a format like: g00B1,110709195723,79328

I'm able to read in my data fine from serial, but this is where I am at with this section:

int strLength = txtMsg.length();
int firstCommaPosition = txtMsg.indexOf(',');
int secondCommaPosition = txtMsg.indexOf(',', firstCommaPosition + 1);
int lastCommaPosition = txtMsg.lastIndexOf(',');
Serial.println(txtMsg.substring(lastCommaPosition+1));

which prints 79328 I dont know how to convert that to a long. Can someone please help?

Thank you, J.C. Woltz

ok, I got to this point:

  String devName = txtMsg.substring(0,firstCommaPosition);
  String DateTime = txtMsg.substring(firstCommaPosition + 1, secondCommaPosition);
  String rawTilt = txtMsg.substring(lastCommaPosition + 1);
  int rTl = rawTilt.length();
  rawTilt.toCharArray(inChar,rTl);
  int tilt = atoi(inChar);
  Serial.println(tilt);
  Serial.println(devName);
  Serial.println(DateTime);

which works as long as my number doesn't go higher than 65535, but my number will go higher than that. Does anyone have advice or a better way to do this?

Thank you, J.C. Woltz

Well, what do you suppose atoi stands for? [u]A[/u]scii [u]to[/u] [u]i[/u]nteger. What would you guess to be the function to convert [u]a[/u]scii [u]to[/u] [u]l[/u]ong?

@PaulS

Thank you. atol works, but you already knew that.

Is there a better way than using strings to break this up? Should I use a character array instead. Please forgive my ignorance on this topic. But I don’t want to run out of ram or have weird to debug issues. While what seems like a common task, I have not had to use C in this way.

[quote author=J.C. Woltz link=topic=66102.msg484615#msg484615 date=1310266821] Is there a better way than using strings to break this up? Should I use a character array instead. Please forgive my ignorance on this topic. But I don't want to run out of ram or have weird to debug issues. While what seems like a common task, I have not had to use C in this way. [/quote]

"Better" depends on your goals. A char array will use less RAM, but it will cause more "weird to debug issues", especially if you haven't used them much. Pick your poison.

A char array will use less RAM, but it will cause more "weird to debug issues", especially if you haven't used them much. Pick your poison.

On the other hand, proper mastery of arrays will serve you well in the future. I'd re-write the code to eliminate the String object(s), and learn to use strtok() and the other string functions.

If you look at the String class, all the real work is done with the string functions.

[quote author=J.C. Woltz link=topic=66102.msg484615#msg484615 date=1310266821]Is there a better way than using strings to break this up? Should I use a character array instead.[/quote]

Here's the advantage of your solution:

  • it's already written
  • it's flexible - as long as you have two commas, what's between them can be any size
  • it's sizable. with a character array, you have to constantly be aware of the end of the array so you don't go past it and trash other values in the program

And disadvantages?

  • it's a bit bigger. typically, a string needs to keep status information (such as length of string), which takes room. if you use a lot of strings, it can add up.
  • you know less about it. the protection the class provides works by hiding details. however, working with a char array exposes all the details, and lets you work 'closer to the metal'. for some code, that can be faster.
  • you are limited to the functions included in the class. char arrays have a rich selection of functions, which can speed up some programming.

In any case, knowledge of char arrays is well worthwhile, but you code works, so maybe you needn't change it.

nobody suggested sscanf?

char name[16]; int i1; long l1;

sscanf(buffer, "%s,%ld,%d", name, &l1, &i1);

or maybe put a size restriction on the first parameter so it doesn't overflow

sscanf is stone-age technology (and a bit piggy) but it works for things like this, maybe compiling smaller than using string classes even...