# Converting String from Particle Subscribe to integer

**URL:** <https://community.particle.io/t/converting-string-from-particle-subscribe-to-integer/47202>\
**Category:** Troubleshooting\
**Tags:** argon, photon\
**Created:** [January 23, 2019, 1:26am UTC](https://community.particle.io/t/converting-string-from-particle-subscribe-to-integer/47202 "2019-01-23T01:26:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TeraBull](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/terabull/32/19717_2.png) [@TeraBull](https://community.particle.io/u/TeraBull)\
**Post date:** [January 23, 2019, 1:26am UTC](https://community.particle.io/t/converting-string-from-particle-subscribe-to-integer/47202/1 "2019-01-23T01:26:58Z")

</div>

I want to collect the temperature from one photon, convert it to a string and publish it to the cloud. Then subscribe to it with my Argon and print it on an LCD. Maybe a string isn’t the best choice, but I was hoping you guys could point me in the right direction.

I am practicing for when I start putting this on a mesh network.

Here is what I have going on in my program:

```auto
//Publishing from Photon:
Particle.publish("DataShare", String(fahrenheit,2), PRIVATE);

//Subscribed from Argon:
// void setup():
Particle.subscribe("DataShare", TemperatureHandler);

//Handler Function:
void TemperatureHandler(const char *event, const char *data){
    Temp = printf( "%d\n", data);
}

//void loop():
    lcd->setCursor(0,1);
    lcd->print("BTemp:");
    lcd->setCursor(5,1);
    lcd->print(Temp);

```

---

<div class="post-metadata">

**Author:** ![nrobinson2000](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/nrobinson2000/32/15624_2.png) [@nrobinson2000](https://community.particle.io/u/nrobinson2000)\
**Post date:** [January 23, 2019, 2:39am UTC](https://community.particle.io/t/converting-string-from-particle-subscribe-to-integer/47202/2 "2019-01-23T02:39:36Z")

</div>

Could you share your **complete** code?

Also, you’re probably going to want to use snprintf() in this situation. With complete code to provide context you’ll see why.

---

<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:** [January 23, 2019, 3:26am UTC](https://community.particle.io/t/converting-string-from-particle-subscribe-to-integer/47202/3 "2019-01-23T03:26:04Z")

</div>

When you subscribe to a `PRIVATE` event your `Particle.subscribe()` scope needs to be `MY_DEVICES` - with device OS v1.0.0 (already since 0.8.0-rc.4 on Gen1&2, for Gen3 this will be true soon) you won’t be allowed to subscribe unscoped.

`printf()` is not a function you can use on these devices because you have no `stdout` channel to print to. As @nrobinson2000 already said, you can only use `sprintf()` or better/safer `snprintf()` to store formatted strings into a buffer for further action, but that’s also not what you want, since the return value of `printf()`, `sprintf()` and `snprintf()` would only tell you the number of characters that were “printed” but not the value of the integer you injected into the string.

You want to _convert_ a string into an `int` and for that you’d use `atoi()`.  
For multiple integers you could also use [`sscanf()`](http://www.cplusplus.com/reference/cstdio/sscanf) (unfortunately not for floating point types - although there are workarounds too).

---

<div class="post-metadata">

**Author:** ![TeraBull](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/terabull/32/19717_2.png) [@TeraBull](https://community.particle.io/u/TeraBull)\
**Post date:** [January 25, 2019, 5:12pm UTC](https://community.particle.io/t/converting-string-from-particle-subscribe-to-integer/47202/4 "2019-01-25T17:12:01Z")

</div>

@ScruffR, thanks for the help. I am still struggling with it. the atoi() didn’t work. I have seen where other people have made it work. I feel like it may be due to how I am sending throught the Particle.publish().

I will work on it some more tonight.

---

<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:** [January 25, 2019, 8:15pm UTC](https://community.particle.io/t/converting-string-from-particle-subscribe-to-integer/47202/5 "2019-01-25T20:15:49Z")

</div>

If your incoming data represents a float then you'd use `atof()` instead.  
But since you used `%d` in your `printf()` statement I assumed you'd get an integer.

However, in order to advise we'd need to know _exactly_ what data you are receiving.

Also have you considered this advice?

> [@ScruffR](#):
>
> When you subscribe to a PRIVATE event your Particle.subscribe() scope needs to be MY\_DEVICES
