Skip to main content
typos, text
Source Link
Codebreaker007
  • 1.3k
  • 1
  • 7
  • 14

Do not work with the String class on Arduino. That said what to do?
Use char arrays (what an individual String is internally). Then you work with pointers or pointers to pointers. Guessing from the fragments (what type is MycopyString?) you gave, you create a pointer (?) to the element in the array.
Working with String class has two disadvantages.

  • You have to look into the lib to see what a function really does and
  • by reserving and releasing memory it fractures on the long (most of the time short) run your heap causing your AVR/ESP to crash.

The possibility to create two dimensional arrays

i = 257;
char myTwoDimArray [i][64] = {'\0','\0'}// 64 ... 63 chars + 1 terminator(!) 

and then you operate with pointers either to the entry (i) or in the specified char array itself.

How to remove an entry You just write: i = 23; myTwoDimArray [i][0] = '\0';

i = 23;
myTwoDimArray [i][0] = '\0';

by writing a zero terminator to the first index (starts with 0) its deleted of the 24th (!) element (again the index starts with 0) its deleted.

Commands to look for when operating with chars

strcpy (targetChar, charToAdd); // Initializes the targetChar starts at index 0
strcat (targetChar, charToAdd); // Appends to the targetChar starts at current index

converting numeric values to chars:

  char numBuffer [16] = {'\0'}; // Helper buffer for conversions

 itoa (myNumberValue,numBuffer,10);  // converts an integer to a base 10 (decimal) char
itoa (myNumberValue,numBuffer,2);  // converts an integer to a base 2 (binary) char
itoa (myNuberValue,numBuffer,16);  // converts an integer to a base 16 (hex) char

itoa initilizes a char array so we need the helper array:

strcat (targetChar, numBuffer);    

to convert float we use

dtostrf(floatVariable, StringLengthIncDecimalPoint, numVarsAfterDecimal, numBuffer);

to convert chars back to int use

int16_t myIntVar = atoi(numBuffer); 

to convert chars back to floats use

float myFloatVar = atof(numBuffer, decimalsToShow); // using just atof(numBuffer)
gives you standard x.XX only 2 decimals

Hope this comprehensive intro to chars helps you for your current and future projects.

Do not work with the String class on Arduino. That said what to do?
Use char arrays (what an individual String is internally). Then you work with pointers or pointers to pointers. Guessing from the fragments (what type is MycopyString?) you gave, you create a pointer (?) to the element in the array.
Working with String class has two disadvantages.

  • You have to look into the lib to see what a function really does and
  • by reserving and releasing memory it fractures on the long (most of the time short) run your heap causing your AVR/ESP to crash.

The possibility to create two dimensional arrays

i = 257;
char myTwoDimArray [i][64] = {'\0','\0'}// 64 ... 63 chars + 1 terminator(!) 

and then you operate with pointers either to the entry (i) or in the specified char array itself.

How to remove an entry You just write: i = 23; myTwoDimArray [i][0] = '\0';

by writing a zero terminator to the first index (starts with 0) its deleted of the 24th (!) element (again the index starts with 0).

Commands to look for when operating with chars

strcpy (targetChar, charToAdd); // Initializes the targetChar starts at index 0
strcat (targetChar, charToAdd); // Appends to the targetChar starts at current index

converting numeric values to chars:

  char numBuffer [16] = {'\0'}; // Helper buffer for conversions

 itoa (myNumberValue,numBuffer,10);  // converts an integer to a base 10 (decimal) char
itoa (myNumberValue,numBuffer,2);  // converts an integer to a base 2 (binary) char
itoa (myNuberValue,numBuffer,16);  // converts an integer to a base 16 (hex) char

itoa initilizes a char array so we need the helper array:

strcat (targetChar, numBuffer);    

to convert float we use

dtostrf(floatVariable, StringLengthIncDecimalPoint, numVarsAfterDecimal, numBuffer);

to convert chars back to int use

int16_t myIntVar = atoi(numBuffer); 
float myFloatVar = atof(numBuffer, decimalsToShow); // using just atof(numBuffer)
gives you standard x.XX only 2 decimals

Hope this comprehensive intro to chars helps you for your current and future projects.

Do not work with the String class on Arduino. That said what to do?
Use char arrays (what an individual String is internally). Then you work with pointers or pointers to pointers. Guessing from the fragments (what type is MycopyString?) you gave, you create a pointer (?) to the element in the array.
Working with String class has two disadvantages.

  • You have to look into the lib to see what a function really does and
  • by reserving and releasing memory it fractures on the long (most of the time short) run your heap causing your AVR/ESP to crash.

The possibility to create two dimensional arrays

i = 257;
char myTwoDimArray [i][64] = {'\0','\0'}// 64 ... 63 chars + 1 terminator(!) 

and then you operate with pointers either to the entry (i) or in the specified char array itself.

How to remove an entry You just write:

i = 23;
myTwoDimArray [i][0] = '\0';

by writing a zero terminator to the first index (starts with 0) of the 24th (!) element (again the index starts with 0) its deleted.

Commands to look for when operating with chars

strcpy (targetChar, charToAdd); // Initializes the targetChar starts at index 0
strcat (targetChar, charToAdd); // Appends to the targetChar starts at current index

converting numeric values to chars:

  char numBuffer [16] = {'\0'}; // Helper buffer for conversions

 itoa (myNumberValue,numBuffer,10);  // converts an integer to a base 10 (decimal) char
itoa (myNumberValue,numBuffer,2);  // converts an integer to a base 2 (binary) char
itoa (myNuberValue,numBuffer,16);  // converts an integer to a base 16 (hex) char

itoa initilizes a char array so we need the helper array:

strcat (targetChar, numBuffer);    

to convert float we use

dtostrf(floatVariable, StringLengthIncDecimalPoint, numVarsAfterDecimal, numBuffer);

to convert chars back to int use

int16_t myIntVar = atoi(numBuffer);

to convert chars back to floats use

float myFloatVar = atof(numBuffer, decimalsToShow); // using just atof(numBuffer)
gives you standard x.XX only 2 decimals

Hope this comprehensive intro to chars helps you for your current and future projects.

Source Link
Codebreaker007
  • 1.3k
  • 1
  • 7
  • 14

Do not work with the String class on Arduino. That said what to do?
Use char arrays (what an individual String is internally). Then you work with pointers or pointers to pointers. Guessing from the fragments (what type is MycopyString?) you gave, you create a pointer (?) to the element in the array.
Working with String class has two disadvantages.

  • You have to look into the lib to see what a function really does and
  • by reserving and releasing memory it fractures on the long (most of the time short) run your heap causing your AVR/ESP to crash.

The possibility to create two dimensional arrays

i = 257;
char myTwoDimArray [i][64] = {'\0','\0'}// 64 ... 63 chars + 1 terminator(!) 

and then you operate with pointers either to the entry (i) or in the specified char array itself.

How to remove an entry You just write: i = 23; myTwoDimArray [i][0] = '\0';

by writing a zero terminator to the first index (starts with 0) its deleted of the 24th (!) element (again the index starts with 0).

Commands to look for when operating with chars

strcpy (targetChar, charToAdd); // Initializes the targetChar starts at index 0
strcat (targetChar, charToAdd); // Appends to the targetChar starts at current index

converting numeric values to chars:

  char numBuffer [16] = {'\0'}; // Helper buffer for conversions

 itoa (myNumberValue,numBuffer,10);  // converts an integer to a base 10 (decimal) char
itoa (myNumberValue,numBuffer,2);  // converts an integer to a base 2 (binary) char
itoa (myNuberValue,numBuffer,16);  // converts an integer to a base 16 (hex) char

itoa initilizes a char array so we need the helper array:

strcat (targetChar, numBuffer);    

to convert float we use

dtostrf(floatVariable, StringLengthIncDecimalPoint, numVarsAfterDecimal, numBuffer);

to convert chars back to int use

int16_t myIntVar = atoi(numBuffer); 
float myFloatVar = atof(numBuffer, decimalsToShow); // using just atof(numBuffer)
gives you standard x.XX only 2 decimals

Hope this comprehensive intro to chars helps you for your current and future projects.