# Parse a String received

**URL:** <https://community.particle.io/t/parse-a-string-received/41297>\
**Category:** Firmware\
**Created:** [April 21, 2018, 2:53am UTC](https://community.particle.io/t/parse-a-string-received/41297 "2018-04-21T02:53:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jimmie](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@Jimmie](https://community.particle.io/u/Jimmie)\
**Post date:** [April 21, 2018, 2:53am UTC](https://community.particle.io/t/parse-a-string-received/41297/1 "2018-04-21T02:53:05Z")

</div>

I am not much of a C programmer and have completed a big sketch except for one remaining step, namely parsing a string delimited by a comma “,”.

The string is received by the following sub:

void handle\_sent\_data(uint8\_t _data, uint16\_t length) {  
Particle.publish(“Data Capture”, String((char_)data));  
}

I know I am receiving the correct string because I can see it on the web but how do I go about parsing it?

Here is a sample string: **“%%,8,7135,4563,5687,@”** I want to store the 8, 7135, 4563 and 5687 in int variables.

Any help is appreciated.

---

<div class="post-metadata">

**Author:** ![Ric](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ric/32/514_2.png) [@Ric](https://community.particle.io/u/Ric)\
**Post date:** [April 21, 2018, 3:07am UTC](https://community.particle.io/t/parse-a-string-received/41297/2 "2018-04-21T03:07:26Z")

</div>

You should check out the C function **strtok()** for separating the string into its pieces, and **atoi()** to convert the string pieces to ints. There should be quite a few references to those here on the forum.

---

<div class="post-metadata">

**Author:** ![Jimmie](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@Jimmie](https://community.particle.io/u/Jimmie)\
**Post date:** [April 21, 2018, 3:36am UTC](https://community.particle.io/t/parse-a-string-received/41297/3 "2018-04-21T03:36:44Z")

</div>

Thank you @Ric.

I looked up strtok() but it is complicated. Any chance you can direct me to a simple example for this string

“%%,8,7135,4563,5687,@” which is stored in the variable data.

Thanks again.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [April 21, 2018, 5:34am UTC](https://community.particle.io/t/parse-a-string-received/41297/4 "2018-04-21T05:34:31Z")

</div>

For non-floating point tokens you could also use `sscanf()`.

```cpp
  int a, b, c, d;
  int result = sscanf(data, "%%%%,%d,%d,%d,%d,@", &a, &b, &c, &d);

```

The four pecent signs represent the _escaped_ “double percent” starting tag. The result value will provide the count of successfully parsed integers.

This also makes sure _not_ to parse unrelated data (not part of the incoming string or strings _not_ contained between the dilimter tags `%%....@`)

---

<div class="post-metadata">

**Author:** ![BulldogLowell](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bulldoglowell/32/4305_2.png) [@BulldogLowell](https://community.particle.io/u/BulldogLowell)\
**Post date:** [April 21, 2018, 5:46am UTC](https://community.particle.io/t/parse-a-string-received/41297/5 "2018-04-21T05:46:42Z")

</div>

> [@Jimmie](#):
>
> “%%,8,7135,4563,5687,@”

you can try like this:

```cpp
struct PacketData {
  int first;
  int second;
  int third;
  int fourth;
};

void setup() {
  Serial.begin(9600);
}

 void loop() {
  const char* inString = "%%,8,7135,4563,5687,@";
  PacketData newData;
  parsePacket(inString, newData);
  Serial.println(newData.first);
  Serial.println(newData.second);
  Serial.println(newData.third);
  Serial.println(newData.fourth);
  delay(5000);
 }

 void parsePacket(const char* str, PacketData& data) {
  char* ptr = str; // create a pointer pointed to the first position in the string
  ptr = strchr(ptr, ','); // move the pointer to the first comma
  ptr++; // move the pointer one position
  data.first = atoi(ptr); // parse the value as an int
  ptr = strchr(ptr, ','); // move the pointer to the next comma, etc
  ptr++;
  data.second = atoi(ptr);
  ptr = strchr(ptr, ',');
  ptr++;
  data.third = atoi(ptr);
  ptr = strchr(ptr, ',');
  ptr++;
  data.fourth = atoi(ptr);
 }

```

---

<div class="post-metadata">

**Author:** ![Jimmie](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@Jimmie](https://community.particle.io/u/Jimmie)\
**Post date:** [April 21, 2018, 2:31pm UTC](https://community.particle.io/t/parse-a-string-received/41297/6 "2018-04-21T14:31:18Z")

</div>

Thank you @ScruffR.

I am getting the error:  
`invalid conversion from 'uint8_t* {aka unsigned char*}' to 'const char*' [-fpermissive]`

Here is my code:

```
void parseData(uint8_t *data, uint16_t length)
//$$,5455,0,1,0,1,1,0,^
{   
  int a, b, c, d, e, f, g;
  sscanf(data, "%%$$,%d,%d,%d,%d,%d,%d,%d,^", &a, &b, &c, &d, &e, &f, &g);

```

}

---

<div class="post-metadata">

**Author:** ![Jimmie](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@Jimmie](https://community.particle.io/u/Jimmie)\
**Post date:** [April 21, 2018, 2:31pm UTC](https://community.particle.io/t/parse-a-string-received/41297/7 "2018-04-21T14:31:42Z")

</div>

Thank you @BulldogLowell. I appreciate it.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [April 21, 2018, 3:07pm UTC](https://community.particle.io/t/parse-a-string-received/41297/8 "2018-04-21T15:07:03Z")

</div>

> [@Jimmie](#):
>
> invalid conversion from 'uint8\_t\* {aka unsigned char\*}' to 'const char\*' [-fpermissive]

To overcome that you could either declare your function as `void parseData(const char* data, uint16_t length)` or do a typecast in the call `sscanf((const char*)data, ...)`.

If your string _now_ starts with two dollar signs (`$$`) you will have to remove the two percent signs in your format string otherwise your string would be expected to start with `"%$$...."`.

---

<div class="post-metadata">

**Author:** ![Jimmie](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@Jimmie](https://community.particle.io/u/Jimmie)\
**Post date:** [April 21, 2018, 3:34pm UTC](https://community.particle.io/t/parse-a-string-received/41297/9 "2018-04-21T15:34:13Z")

</div>

> [@ScruffR](#):
>
> (const char\*)data

Thank you @ScruffR.
