system
October 29, 2014, 12:31am
1
I have a variable would lost in the Program. Anyone can tell me what is the problem?
String temp1 = "";
String temp2 = "";
int x=1;
void setup() {
Serial.begin(115200); // initialize serial:
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
}
void loop()
{
if (x == 1) {
temp1 = "";
temp1 += char(0xEC);
temp1 += char(0x03);
temp1 += char(0x00);
temp1 += char(0x49);
temp2 = temp1;
Serial.println(temp1);
Serial.println(temp2);
Serial.println(String(temp1[0], DEC) + " , " +String(temp2[0], DEC));
Serial.println(String(temp1[1], DEC) + " , " +String(temp2[1], DEC));
Serial.println(String(temp1[2], DEC) + " , " +String(temp2[2], DEC));
Serial.println(String(temp1[3], DEC) + " , " +String(temp2[3], DEC));
x= 0;
}
}
The Result
ì I
ì 2
-20 , -20
3 , 3
0 , 0
73 , 50
Q1. Why the Result of temp1 is not same as temp2.
Q2. When I try to test the temp2[3] value, the value was disapper. Sometime when I try to change the program format, the temp2[3] would change to any value.
Many Thanks
Post the rest of your code, then people will be able to tell you what is wrong.
system
October 29, 2014, 12:40am
3
jremington, Already post the rest of code.
system
October 29, 2014, 12:49am
4
temp1 += char(0x00);
You are embedding a NULL (otherwise known as a stop sign) in the String.
temp2 = temp1;
Only the data up to the NULL is copied.
Q1. Why the Result of temp1 is not same as temp2.
See above.
Serial.println(String(temp1[0], DEC) + " , " +String(temp2[0], DEC));
Are you really too lazy to use three Serial.print() statements. Pissing away resources like this is silly.
So is printing anonymous data. If you don't care enough to print some identifier before the data, don't bother printing the data.
system
October 29, 2014, 1:05am
5
Thanks PaulS,
Is any way using (0x00) (Null) in the String?
My Main Project is doing Encrypy and Decrypy.
Some time these program would translate the data to (0x00).
Because I am a newbit in Arduino. Have you any ideal for me?
I just want the temp2 = temp1 was safety sucessfully.
p.s. the three Serial.print() for show off the problems.
system
October 29, 2014, 1:07am
6
Is any way using (0x00) (Null) in the String?
The easiest way (also the best way) is to put the crutches away and quit using the String class. NULL, or not, terminated arrays of chars are far better for use on the limited-memory Arduino.
andrewchoi:
My Main Project is doing Encrypy and Decrypy.
Some time these program would translate the data to (0x00).
Use an array, eg.
byte myData [20]; // or however long it needs to be
system
October 29, 2014, 1:16am
8
Hi PaulS,
I got your meaning. I think I need to change my program framework.
I know what is Null in the String now. <<--- Before I don't know what is it?
Thank you very much!
system
October 29, 2014, 11:10am
10
Thanks Paul and Nick Gammon,
Byte type Array fixed my Problem.
I think I need upgrade my C programming Skill.
system
October 30, 2014, 12:17am
11
I think I need upgrade my C programming Skill.
I'm still upgrading mine, after nearly 30 years as a C/C++ programmer.