2016/10/03

Arduino - String Operation - strncpy(), strcpy(), and toCharArray()

Here is an example of how to use strncpy(), strcpy(), and toCharArray().

Code



  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// source: http://forum.arduino.cc/index.php?topic=48876.0

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

   // Copy pre-defined (constant) string into char array using strncpy()

   int numChars = 30;
   char textArray1[numChars];

   strncpy(textArray1, "01234567890123456789", numChars);
   Serial.print("textArray1 content using strncpy(): ");
   Serial.println(textArray1);

   for (int i = 0; i < sizeof(textArray1); i++) {
    Serial.print(i);
    Serial.print(". ");
    Serial.print(textArray1[i]);
    Serial.print(" ");
    Serial.println((int)textArray1[i]);
   }

   // Copy pre-defined (constant) string into char array using strcpy()
   // source:

   char textArray2[numChars];
   
   strcpy(textArray2, "01234567890123456789");
   Serial.println("---------------------------------------------------------------");
   Serial.print("textArray2 content using strcpy(): ");
   Serial.println(textArray2); 

   for (int i = 0; i < sizeof(textArray2); i++) {
    Serial.print(i);
    Serial.print(". ");
    Serial.print(textArray2[i]);
    Serial.print(" ");
    Serial.println((int)textArray2[i]);
   }

   // Copy string variable into char array using toCharArray()
   // source: https://www.arduino.cc/en/Reference/StringToCharArray
    
   char textArray3[numChars];
                                  
   String str = "012345678901234567890123456789";

   str.toCharArray(textArray3, numChars);
   Serial.println("---------------------------------------------------------------");
   Serial.print("textArray3 content: ");
   Serial.println(textArray3); 

   for (int i = 0; i < sizeof(textArray3); i++) {
    Serial.print(i);
    Serial.print(". ");
    Serial.print(textArray3[i]);
    Serial.print(" ");
    Serial.println((int)textArray3[i]);
   }

   // Copy string variable made up of multiple string variables into char array using toCharArray()
   // source: https://www.arduino.cc/en/Reference/StringToCharArray
    
   char textArray4[numChars];

   String strA;
   String str1 = "ABC -";
   String str2 = "DEF -";
   String str3 = "GHIJK -";
   String str4 = "1234567890 -";

   strA = str1 + str2 + str3 + str4;

   //strA.toCharArray(textArray4, numChars-1);
   strA.toCharArray(textArray4, numChars);
   Serial.println("---------------------------------------------------------------");
   Serial.print("str1 = ");
   Serial.println(str1);
   Serial.print("str2 = ");
   Serial.println(str2);
   Serial.print("str3 = ");
   Serial.println(str3);
   Serial.print("str4 = ");
   Serial.println(str4);

   Serial.print("textArray4 content: ");
   Serial.println(textArray4); 

   for (int i = 0; i < sizeof(textArray4); i++) {
    Serial.print(i);
    Serial.print(". ");
    Serial.print(textArray4[i]);
    Serial.print(" ");
    Serial.println((int)textArray4[i]);
   }

   // Copy string variable made up of multiple string variables into char array using toCharArray()
   // The 1st string variable to be copied has \0 in it to test out the string termination symbol
   // source: https://www.arduino.cc/en/Reference/StringToCharArray
    
   char textArray5[numChars];

   String strB;
   String str5 = "1\0 23 -";
   String str6 = "456 -";
   String str7 = "7890 -";
   String str8 = "1234567890 -";

   strB = str5 + str6 + str7 + str8;

   strB.toCharArray(textArray5, numChars);
   Serial.println("---------------------------------------------------------------");
   Serial.print("str5 = ");
   Serial.println(str5);
   Serial.print("str6 = ");
   Serial.println(str6);
   Serial.print("str7 = ");
   Serial.println(str7);
   Serial.print("str8 = ");
   Serial.println(str8);

   Serial.print("textArray5 content: ");
   Serial.println(textArray5); 

   for (int i = 0; i < sizeof(textArray5); i++) {
    Serial.print(i);
    Serial.print(". ");
    Serial.print(textArray5[i]);
    Serial.print(" ");
    Serial.println((int)textArray5[i]);
   }     
}

void loop()
{

}

Output

// Copy pre-defined (constant) string into char array using strncpy()


Note, the strncpy() fills all the unused space in the char array with '\0'.

// Copy pre-defined (constant) string into char array using strcpy()


Note, the strcpy() only adds one '\0' at the end of the string and leave the rest of the unused buffer space as is.

// Copy string variable into char array using toCharArray()


// Copy string variable made up of multiple string variables into char array using toCharArray()


// Copy string variable made up of multiple string variables into char array using toCharArray()
// The 1st string variable to be copied has \0 in it to test out the string termination symbol


Additional note about toCharArray()

Similar to strcpy(), toCharArray() only adds one '\0' at the end of the string and leave the unused space untouched.



References:

toCharArray()
https://www.arduino.cc/en/Reference/StringToCharArray

Topic: Load new string into char array?
http://forum.arduino.cc/index.php?topic=44362.0

No comments:

Post a Comment