# How to convert string to char?

**URL:** <https://community.particle.io/t/how-to-convert-string-to-char/14218>\
**Category:** Troubleshooting\
**Created:** [August 8, 2015, 7:01am UTC](https://community.particle.io/t/how-to-convert-string-to-char/14218 "2015-08-08T07:01:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fangda01](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/fangda01/32/4946_2.png) [@fangda01](https://community.particle.io/u/fangda01)\
**Post date:** [August 8, 2015, 7:01am UTC](https://community.particle.io/t/how-to-convert-string-to-char/14218/1 "2015-08-08T07:01:10Z")

</div>

String testString = “{“token”:123456789,“barcode”:[123456789,987654321],“quantity”:[1,5]}”;

char \*testChar;

testChar \<\<\<\< testString; ???

Because i want to use JSMN Library to Json parser  
[JSON parser for Spark](https://community.particle.io/t/json-parser-for-spark/2838)  
😃

---

<div class="post-metadata">

**Author:** ![chrisp](https://avatars.discourse-cdn.com/v4/letter/c/87869e/32.png) [@chrisp](https://community.particle.io/u/chrisp)\
**Post date:** [August 8, 2015, 7:59am UTC](https://community.particle.io/t/how-to-convert-string-to-char/14218/2 "2015-08-08T07:59:25Z")

</div>

Check out this post ([Are string variables fully supported? [SOLVED]](https://community.particle.io/t/are-string-variables-fully-supported-solved/3332)) for some examples. For more info on the toCharArray() method see this link: [https://www.arduino.cc/en/Reference/StringToCharArray](https://www.arduino.cc/en/Reference/StringToCharArray)

---

<div class="post-metadata">

**Author:** ![fangda01](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/fangda01/32/4946_2.png) [@fangda01](https://community.particle.io/u/fangda01)\
**Post date:** [August 8, 2015, 9:41am UTC](https://community.particle.io/t/how-to-convert-string-to-char/14218/3 "2015-08-08T09:41:19Z")

</div>

Syntax

string.toCharArray(buf, len)

String testString = “{“token”:123456789,“barcode”:[123456789,987654321],“quantity”:[1,5]}”;

char \*testChar;  
testString.toCharArray(testChar, ?) how I know that I have to put that value. ?

---

<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:** [August 8, 2015, 10:50am UTC](https://community.particle.io/t/how-to-convert-string-to-char/14218/4 "2015-08-08T10:50:38Z")

</div>

What exactly do you want to do with your string?  
If you just want work with it as `const char*` or pass it to a function that expects a `const char*`, you could use `string.c_str()`.  
If you want to copy the string into a char array, you can use `string.toCharArray()` together with `string.length()`.  
But you’d need a `char[]` big enough to take all your copied characters. `char*` does not allocate any RAM to store the string to.

But you could also look at the docs  
[https://docs.particle.io/reference/firmware/photon/#string-class](https://docs.particle.io/reference/firmware/photon/#string-class)

---

<div class="post-metadata">

**Author:** ![bko](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bko/32/20704_2.png) [@bko](https://community.particle.io/u/bko)\
**Post date:** [August 8, 2015, 3:01pm UTC](https://community.particle.io/t/how-to-convert-string-to-char/14218/5 "2015-08-08T15:01:35Z")

</div>

> [@fangda01](#):
>
> String testString = "{"token":123456789,"barcode":[123456789,987654321],"quantity":[1,5]}";
> 
> char \*testChar;  
> testString.toCharArray(testChar, ?) how I know that I have to put that value. ?

I just want to clear up one point. When you do `char *testChar;` above, that does not allocate any storage for the array of characters, so the next line which does essentially a memcpy, will clobber whatever comes after that pointer in memory.

Do this instead:

```cpp
String testString = "{\"token\":123456789,\"barcode\":[123456789,987654321],\"quantity\":[1,5]}";

char testChar[65];
testString.toCharArray(testChar, 65);

```

---

<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:** [August 8, 2015, 7:33pm UTC](https://community.particle.io/t/how-to-convert-string-to-char/14218/6 "2015-08-08T19:33:36Z")

</div>

> [@ScruffR](#):
>
> But you'd need a char big enough to take all your copied characters. char\* does not allocate any RAM to store the string to.

@bko, this can not be said often enough 😉

---

<div class="post-metadata">

**Author:** ![bko](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bko/32/20704_2.png) [@bko](https://community.particle.io/u/bko)\
**Post date:** [August 8, 2015, 9:04pm UTC](https://community.particle.io/t/how-to-convert-string-to-char/14218/7 "2015-08-08T21:04:26Z")

</div>

Sorry I missed that in your response above! You were there before me again!

---

<div class="post-metadata">

**Author:** ![RWB](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rwb/32/22599_2.png) [@RWB](https://community.particle.io/u/RWB)\
**Post date:** [April 22, 2018, 6:08am UTC](https://community.particle.io/t/how-to-convert-string-to-char/14218/8 "2018-04-22T06:08:20Z")

</div>

> [@bko](#):
>
> String testString = "{"token":123456789,"barcode":[123456789,987654321],"quantity":[1,5]}";
> 
> char testChar[65];  
> testString.toCharArray(testChar, 65);

Thanks! This worked perfectly for converting the time returned in string format via Particles Time.timeStr() function to a char array to display on an LCD screen that requires the data in Char format.
