# How to convert char to string

**URL:** <https://community.particle.io/t/how-to-convert-char-to-string/7304>\
**Category:** General\
**Created:** [October 3, 2014, 8:07pm UTC](https://community.particle.io/t/how-to-convert-char-to-string/7304 "2014-10-03T20:07:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kw123](https://avatars.discourse-cdn.com/v4/letter/k/46a35a/32.png) [@kw123](https://community.particle.io/u/kw123)\
**Post date:** [October 3, 2014, 8:07pm UTC](https://community.particle.io/t/how-to-convert-char-to-string/7304/1 "2014-10-03T20:07:43Z")

</div>

i have an input buffer as a CHAR \* variable, but would like to convert that to a STRING variable. For me its easier to handle the string manipulations with the given string functions…

Could not find anything in the docs or the discussion forums.

I know there must be a trivial way to do this.

thx

KW

---

<div class="post-metadata">

**Author:** ![Moors7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/moors7/32/588_2.png) [@Moors7](https://community.particle.io/u/Moors7)\
**Post date:** [October 3, 2014, 8:40pm UTC](https://community.particle.io/t/how-to-convert-char-to-string/7304/2 "2014-10-03T20:40:33Z")

</div>

You could give this a try: [http://www.dotnetperls.com/convert-char-array-string](http://www.dotnetperls.com/convert-char-array-string)

---

<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:** [October 3, 2014, 9:27pm UTC](https://community.particle.io/t/how-to-convert-char-to-string/7304/3 "2014-10-03T21:27:32Z")

</div>

Have you tried assigning the `char*` variable to the `String`?

There is an overload `String & operator = (const char *cstr);`, so this should work:

```auto
    char cStr[10] = "Test"; // this way cStr is of type char*
    String sStr = cStr; 

```

---

<div class="post-metadata">

**Author:** ![kw123](https://avatars.discourse-cdn.com/v4/letter/k/46a35a/32.png) [@kw123](https://community.particle.io/u/kw123)\
**Post date:** [October 3, 2014, 9:39pm UTC](https://community.particle.io/t/how-to-convert-char-to-string/7304/4 "2014-10-03T21:39:13Z")

</div>

yes thanks that is it

I guess this does not copy, but the char / string accesses the same memory cells?

[EDIT] works perfectly, thanks

KW

---

<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:** [October 4, 2014, 5:41am UTC](https://community.particle.io/t/how-to-convert-char-to-string/7304/5 "2014-10-04T05:41:39Z")

</div>

> [@kw123](#):
>
> I guess this does not copy, but the char / string accesses the same memory cells?

No, it actually does copy the characters to a new location on the heap.

Look at the implementation of the assignment operator (you find this in the open source section)

```auto
String & String::copy(const char *cstr, unsigned int length)
{
	if (!reserve(length)) {
		invalidate();
		return *this;
	}
	len = length;
	strcpy(buffer, cstr);
	return *this;
}
...
String & String::operator = (const char *cstr)
{
	if (cstr) copy(cstr, strlen(cstr));
	else invalidate();
	
	return *this;
}

```
