Hi, my previous topic is really helpful for me and anyone who want to understand basicly about the pointers. But it's messy if I continue to ask another thing about the pointers, so i've splited it up now.
I have a small function in the big program (I think it's not nessesary to show you all because they are not make sense, but If you want, just tell me and I will show it up):
int readParam(String* request)
{
// This handles a hex digit 0 to F (0 to 15).
char buffer[2];
buffer[0] = request->charAt(1);
buffer[1] = 0;
return (int) strtol(buffer, NULL, 16);
}
What is the "buffer" and Why it's color is orange (mean something serious), what is it role in this case?
As AWOL pointed out, the colors really don't have significance themselves. It's done to show the difference between keywords, functions, etc. I have changed the colors of mind since there are certain colors I cannot see. If you go to the directory where you installed your compiler, the lib/theme subdirectory has a file named theme.txt that you can use to change the colors used in the IDE.
In your post, strtol() is a function that is used to convert a string to a long data type. It is designed to work with C string data types, not a String object. Note the lower and upper case 's' differences. The String class can be used to create a String object, which can then be manipulated by methods (i.e., functions) that are defined within the String class. The variable request is a String object and your code is using the charAt() method within the String class.
Many of us void using the String class because it bloats your code above what can be done with a simple C string. In your code:
void setup() {
// put your setup code here, to run once:
int val;
// String test = "1234";
char test[] = "1234";
Serial.begin(9600);
Serial.print("test = ");
Serial.println(test);
val = readParam(test);
Serial.print("val = ");
Serial.println(val);
}
/* The C string version: */
int readParam(char* request)
{
// This handles a hex digit 0 to F (0 to 15).
char buffer[2];
buffer[0] = request[1];
buffer[1] = NULL; // better documentation of intent
return (int) strtol(buffer, NULL, 16);
}
/* The Sstring version:
int readParam(String* request)
{
// Using String class: This handles a hex digit 0 to F (0 to 15).
char buffer[2];
buffer[0] = request->charAt(1);
buffer[1] = 0;
return (int) strtol(buffer, NULL, 16);
}
*/
void loop() {
}
the String version uses 3640 bytes of code. The C string version uses 2542 bytes of code, but produces the same answer. You can find a list of the C string processing functions at:
Edit:
Some of the C string data conversion types are found here:
But, I understand that the function readParam is just cut out the 2nd character (and only one character) of the input string and convert it to long integer. But what if I change the char buffer[2] to char buffer[1] as the code:
void setup() {
// put your setup code here, to run once:
int val;
// char test[] = "1234";
String test = "1234";
Serial.begin(9600);
Serial.print("test = ");
Serial.println(test);
val = readParam(&test);
Serial.print("val = ");
Serial.println(val);
}
int readParam(String* request)
{
// Using String class: This handles a hex digit 0 to F (0 to 15).
char buffer[1]; //we just need 1 character
buffer[0] = request->charAt(1);
//buffer[1] = 0; //cut it off
return (int) strtol(buffer, NULL, 16);
}
void loop() {
}
The answer is still "val = 2" (the same result of the original readParam fuction), so did I go the right way?
ductruong253:
int readParam(String* request)
{
// Using String class: This handles a hex digit 0 to F (0 to 15).
char buffer[1]; //we just need 1 character
buffer[0] = request->charAt(1); // You're still asking for the 2nd character
//buffer[1] = 0; //cut it off
return (int) strtol(buffer, NULL, 16);
}
The answer is still "val = 2" (the same result of the original readParam fuction), so did I go the right way?
Changing the size of the buffer does not change the assignment statement above. The function would be more flexible if you changed it to:
/*****
Purpose: To exact a given character from a string, convert it to an int
Parameter list:
char *request an array of hex digit characters
int fetchMe the character position in the array I want to fetch
Return value
int the value or -1 on error
*****/
int readParam(char* request, int fetchMe)
{
// This handles a hex digit 0 to F (0 to 15).
char buffer[2];
buffer[0] = request[fetchMe];
if (ishexdigit(buffer[0])) {
buffer[1] = NULL; // better documentation of intent
return (int) strtol(buffer, NULL, 16);
} else {
return -1; // Error
}
}
Why are you still using the String object instead of a C string?
econjack:
Why are you still using the String object instead of a C string?
The problem is not why I use C string or String object, it's about I really don't understand why we declare the buffer with 2 character but we only need 1?
And another thing is "buffer[1]=NULL", and the buffer[0]="1" (for example), then what is the full buffer value?
Or did I misconcept about the char type and string type?