# toCharArray Giving Weird Results

**URL:** <https://forum.arduino.cc/t/tochararray-giving-weird-results/966905>\
**Category:** Programming\
**Created:** [March 7, 2022, 10:16pm UTC](https://forum.arduino.cc/t/tochararray-giving-weird-results/966905 "2022-03-07T22:16:29Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![mr\_tropica](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@mr\_tropica](https://forum.arduino.cc/u/mr_tropica)\
**Post date:** [March 7, 2022, 10:16pm UTC](https://forum.arduino.cc/t/tochararray-giving-weird-results/966905/1 "2022-03-07T22:16:29Z")

</div>

I have a String that I want to convert to a char Array.  
The following code WORKS FINE

```cpp
char buff[100] = "";

strcpy(buff, "MyString5522");

```

However THE FOLLOWING gives me a weird block as the beginning character.

```cpp
char buff[100];
String myString = "MyString5522";

myString.toCharArray(buff, myString.length()+1);

```

How can I fix this?

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [March 7, 2022, 10:24pm UTC](https://forum.arduino.cc/t/tochararray-giving-weird-results/966905/2 "2022-03-07T22:24:37Z")

</div>

> [@mr\_tropica](#):
>
> THE FOLLOWING gives me a weird block as the beginning character.

How and where are you seeing this weird character ? Please post a small but complete sketch that illustrates your problem

---

<div class="post-metadata">

**Author:** ![mr\_tropica](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@mr\_tropica](https://forum.arduino.cc/u/mr_tropica)\
**Post date:** [March 7, 2022, 10:29pm UTC](https://forum.arduino.cc/t/tochararray-giving-weird-results/966905/3 "2022-03-07T22:29:55Z")

</div>

```cpp
#include<SPI.h>

char buff[100] = "";

String B1SN = "MySerialTest4455";
String B2SN = "";

volatile int pos;
volatile bool active;

void setup()

{
  Serial.begin(9600);
  pinMode(MISO, OUTPUT);
  SPCR |= bit(SPE);
  SPCR |= bit(SPIE);
}

// SPI interrupt routine
ISR (SPI_STC_vect)
{
  byte c = SPDR;

  if (c == 1) // starting new sequence?
  {
     B1SN.toCharArray(buff, B1SN.length()+1);
     active = true;
     pos = 0;
     SPDR = buff [pos++]; // send first byte
     return;
  }
  if (c == 2) // starting new sequence?
  {
     strcpy(buff, "ENERGIZER");
     active = true;
     pos = 0;
     SPDR = buff [pos++]; // send first byte
     return;
  }

  if (!active)
    {
    SPDR = 0;
    return;
    }

  SPDR = buff [pos];
  if (buff [pos] == 0 || ++pos >= sizeof (buff))
    active = false;
    strcpy(buff, "");
}

void loop()
{
  
}

```

This is a slave code for a SPI project

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [March 7, 2022, 10:44pm UTC](https://forum.arduino.cc/t/tochararray-giving-weird-results/966905/4 "2022-03-07T22:44:08Z")

</div>

The obvious question is why are you using a String in the first place ?

I suspect that the problem is not in the toCharArray() function but elsewhere in your code

Try

```cpp
void setup()
{
  Serial.begin(115200);
  char buff[100];
  String myString = "A";
  myString.toCharArray(buff, myString.length() + 1);
  Serial.println(strlen(buff));
  Serial.println(buff);
}

void loop()
{
}
```

---

<div class="post-metadata">

**Author:** ![mr\_tropica](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@mr\_tropica](https://forum.arduino.cc/u/mr_tropica)\
**Post date:** [March 7, 2022, 10:46pm UTC](https://forum.arduino.cc/t/tochararray-giving-weird-results/966905/5 "2022-03-07T22:46:24Z")

</div>

Would it be better to just store everything initially as a char array?  
Does this help with memory for my Arduino?

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [March 7, 2022, 10:48pm UTC](https://forum.arduino.cc/t/tochararray-giving-weird-results/966905/6 "2022-03-07T22:48:11Z")

</div>

Yes to both questions

---

<div class="post-metadata">

**Author:** ![mr\_tropica](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@mr\_tropica](https://forum.arduino.cc/u/mr_tropica)\
**Post date:** [March 7, 2022, 10:54pm UTC](https://forum.arduino.cc/t/tochararray-giving-weird-results/966905/7 "2022-03-07T22:54:33Z")

</div>

Ok I just converted to the char array but still the same problem, if I use strcpy(buff, SN) in the setup, everything works fine but as soon as the strcpy is in the interrupt it gives me this weird block.  
I will send a quick screenshot

---

<div class="post-metadata">

**Author:** ![mr\_tropica](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@mr\_tropica](https://forum.arduino.cc/u/mr_tropica)\
**Post date:** [March 7, 2022, 10:59pm UTC](https://forum.arduino.cc/t/tochararray-giving-weird-results/966905/8 "2022-03-07T22:59:47Z")

</div>

```cpp
#include<SPI.h>

char buff[100] = "";

char B1SN[100] = "MySerialTest5524558Red";

volatile int pos;
volatile bool active;

void setup()

{
  Serial.begin(9600);
  pinMode(MISO, OUTPUT);
  SPCR |= bit(SPE);
  SPCR |= bit(SPIE);
}

// SPI interrupt routine
ISR (SPI_STC_vect)
{
  byte c = SPDR;

  if (c == 1) // starting new sequence?
  {
     strcpy(buff, B1SN);
     active = true;
     pos = 0;
     SPDR = buff [pos++]; // send first byte
     return;
  }

  if (!active)
    {
    SPDR = 0;
    return;
    }

  SPDR = buff [pos];
  if (buff [pos] == 0 || ++pos >= sizeof (buff))
    active = false;
    strcpy(buff, "");
}

void loop()
{
  
}

```

This gives This...

 ![Ser](https://europe1.discourse-cdn.com/arduino/original/4X/7/0/a/70aba5bccbdc34a4782267dba5048bb8329470f4.png)

---

<div class="post-metadata">

**Author:** ![mr\_tropica](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@mr\_tropica](https://forum.arduino.cc/u/mr_tropica)\
**Post date:** [March 7, 2022, 11:05pm UTC](https://forum.arduino.cc/t/tochararray-giving-weird-results/966905/9 "2022-03-07T23:05:59Z")

</div>

However if i place the strcpy in the setup it does what I expect, but I can't use that method as I need to update the buffer with different info

```cpp
#include<SPI.h>

char buff[100] = "";

char B1SN[100] = "MySerialTest5524558Red";

volatile int pos;
volatile bool active;

void setup()

{
  Serial.begin(9600);
  pinMode(MISO, OUTPUT);
  SPCR |= bit(SPE);
  SPCR |= bit(SPIE);
  strcpy(buff, B1SN);
}

// SPI interrupt routine
ISR (SPI_STC_vect)
{
  byte c = SPDR;

  if (c == 1) // starting new sequence?
  {
     active = true;
     pos = 0;
     SPDR = buff [pos++]; // send first byte
     return;
  }

  if (!active)
    {
    SPDR = 0;
    return;
    }

  SPDR = buff [pos];
  if (buff [pos] == 0 || ++pos >= sizeof (buff))
    active = false;
    strcpy(buff, "");
}

void loop()
{
  
}

```

 ![Ser2](https://europe1.discourse-cdn.com/arduino/original/4X/4/6/8/468fe7aee2421b82f60fd917301acea5c392014f.png)

---

<div class="post-metadata">

**Author:** ![PieterP](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@PieterP](https://forum.arduino.cc/u/PieterP)\
**Post date:** [March 7, 2022, 11:39pm UTC](https://forum.arduino.cc/t/tochararray-giving-weird-results/966905/10 "2022-03-07T23:39:06Z")

</div>

> [@mr\_tropica](#):
>
> `myString.toCharArray(buff, myString.length()+1)`

Bad idea. The second argument to the `toCharArray` method is the size of the _buffer_, not the length of the _string_.

---

<div class="post-metadata">

**Author:** ![mr\_tropica](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@mr\_tropica](https://forum.arduino.cc/u/mr_tropica)\
**Post date:** [March 8, 2022, 12:20am UTC](https://forum.arduino.cc/t/tochararray-giving-weird-results/966905/11 "2022-03-08T00:20:56Z")

</div>

I found the problem.  
On my master i have a delayMicroseconds, increasing this time removes the weird characters and increases the length of sendable data

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [March 8, 2022, 1:24am UTC](https://forum.arduino.cc/t/tochararray-giving-weird-results/966905/12 "2022-03-08T01:24:27Z")

</div>

> [@mr\_tropica](#):
>
> ```arduino
> if (buff [pos] == 0 || ++pos >= sizeof (buff))
> active = false;
> strcpy(buff, "");
> 
> ```

I have not read in details but The indentation in this snippet seems to indicate you wanted to include the second line in the if expression. Are you missing {}?

And to empty a cString you don’t need strcpy() just use `buff[0] = '\0';` or `*buff = 0;` and the likes.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 4, 2022, 1:24am UTC](https://forum.arduino.cc/t/tochararray-giving-weird-results/966905/13 "2022-09-04T01:24:41Z")

</div>

Closed on September 4, 2022, 1:24am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
