How can I put a char into my array or String, since I convert the array to String. Want I want to do is this:
I have 2445421
and want to put "." so i get 2.445421
How can I do that to an array or String?
How can I put a char into my array or String, since I convert the array to String. Want I want to do is this:
I have 2445421
and want to put "." so i get 2.445421
How can I do that to an array or String?
if your data is an a String you could use String.substring() to extract 2 and 445421 to create a new string including the .
I'm not much of a String fan, so here's an alternative:
void setup() {
char msg[10];
char buffer[10];
char insert = '.';
int targetPosition = 1; // Where you want to place the decimal point
Serial.begin(9600);
strcpy(msg, "2445421");
strcpy(buffer, msg);
buffer[targetPosition] = insert;
strcpy(&buffer[targetPosition + 1], &msg[targetPosition]);
Serial.println(buffer);
}
void loop() {
}
Done... Thank you!
using the String class
void setup() {
String s="2445421";
String x=s.substring(0,1) + "." + s.substring(1) ;
Serial.begin(115200);
Serial.println(x);
}
the serial monitor displays
2.445421
Horace's code works perfectly. My C string code uses 1878 bytes of flash memory and 196 bytes of SRAM. Horace's code uses 3548 bytes of flash and 208 bytes of SRAM. The String class is convenient, but is pretty expensive in terms of resource use versus C strings.
I have 2445421
and want to put "." so i get 2.445421
What is the source of the array or String that you want to manipulate ?
If the characters are added one at a time, as seems likely, could you add the extra character when the array is being created ?
void setup() {
char msg[] = "2445421";
char buffer[sizeof(msg) + 1];
const char insert = '.';
byte targetPosition = 1;
Serial.begin(9600);
for (byte i = 0, j = 0; i < sizeof(msg); i++, j++) {
if (i == targetPosition) {
buffer[j++] = insert;
}
buffer[j] = msg[i];
}
Serial.println(buffer);
}
void loop() {}
2.445421
1576 Bytes (5%) Flash, 196 Bytes (9%) RAM.
econjack:
Horace's code works perfectly. My C string code uses 1878 bytes of flash memory and 196 bytes of SRAM. Horace's code uses 3548 bytes of flash and 208 bytes of SRAM. The String class is convenient, but is pretty expensive in terms of resource use versus C strings.
correct! String gives convenience and flexibility when manipulating text strings but like any C++ class tends to be larger and execution time slower than the equivalent C manipulating memory contents directly.
Here is another variation using strcpy()
char msg[] = "2445421";
char buffer[sizeof(msg) + 1]=" .";
// copy first character
buffer[0]=msg[0];
// copy rest of string
strcpy(&buffer[2],&msg[1]);
or using memcpy()
char msg[] = "2445421";
char buffer[sizeof(msg) + 1]=" .";
// copy first character
buffer[0]=msg[0];
// copy rest of string including terminating null
memcpy(&buffer[2],&msg[1], sizeof(msg)-1);
However, String (I assume) carries out bounds checking – it is very easy when copying memory contents directly to overrun arrays corrupting memory.
When using C++, C#, Java, etc on PCs I would use String or equivalent. On a microcontroller I would tend to avoid its use unless I was prototyping code, was short of time and code size and execution time was not critical.