0
Offline
Newbie
Karma: 0
Posts: 11
Arduino rocks
|
 |
« on: May 28, 2011, 11:09:19 am » |
Hi All,
I am having difficulty in converting a String to a Byte array.
Is there an easy way to do this? I have tried and failed several times.
Here is what I want to do.
Take a string, then convert it to bytes. This is so that I can set an IP address using an ethernet shield from a String that is sent to the Arduino.
Thanks
Steve
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
|
 |
« Reply #1 on: May 28, 2011, 11:12:30 am » |
You should be able to locate the delimiters in the String object, using indexOf. Then, you can extract each token, using substring. From the substring, you can use toCharArray to get a NULL terminated array of chars that atoi() can convert to a number that you can store in the byte array, in the appropriate position.
|
|
|
|
« Last Edit: May 28, 2011, 11:15:15 am by PaulS »
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 11
Arduino rocks
|
 |
« Reply #2 on: May 28, 2011, 11:26:36 am » |
Hi Paul, Thanks for your reply. It is the toCharArray that I am having problems with. Here is the sketch. byte ip[]={192,168,2,1}; String strIP="192.17.200.3";
void setup(){ Serial.begin(9600);
}
void loop(){ int index[3]; String string[4]; int a;
index[0]=strIP.indexOf("."); index[1]=strIP.indexOf(".",index[0]+1); index[2]=strIP.indexOf(".",index[1]+1); string[0]=strIP.substring(0,index[0]); string[1]=strIP.substring(index[0]+1,index[1]); string[2]=strIP.substring(index[1]+1,index[2]); string[3]=strIP.substring(index[2]+1); Serial.println("String"); Serial.println(string[0]); Serial.println(string[1]); Serial.println(string[2]); Serial.println(string[3]); char* charIP1[3]; char* charIP2[3]; char* charIP3[3]; char* charIP4[3];
string[0].toCharArray(charIP1[0],3); Serial.println("Start Char"); Serial.println(charIP1[0]);
}
Thanks Steve
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #3 on: May 28, 2011, 11:28:54 am » |
char* charIP1[3]; char* charIP2[3]; char* charIP3[3]; char* charIP4[3];
string[0].toCharArray(charIP1[0],3);
charIP1is an array of three pointers to "char", not an array of "char". char charIP1[4];
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 11
Arduino rocks
|
 |
« Reply #4 on: May 28, 2011, 11:31:20 am » |
Thanks Awol,
I thought I had to use char*, so that I could use atoi later on?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 11
Arduino rocks
|
 |
« Reply #5 on: May 28, 2011, 11:33:29 am » |
Just tried changing it to char from char * and I get an error generated from the toCharArray function, as it says it requires a char* type.
Thanks
Steve
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #6 on: May 28, 2011, 12:27:31 pm » |
Show your code.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 11
Arduino rocks
|
 |
« Reply #7 on: May 28, 2011, 12:59:31 pm » |
Hi Awol,
The code is the same as posted above.
Thanks
Steve
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #8 on: May 28, 2011, 02:02:15 pm » |
You mean this? char* charIP1[3]; char* charIP2[3]; char* charIP3[3]; char* charIP4[3]; Post your code.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 11
Arduino rocks
|
 |
« Reply #9 on: May 28, 2011, 02:17:44 pm » |
Hi Awol, Here is the code. byte ip[]={192,168,2,1}; String strIP="192.17.200.3";
void setup(){ Serial.begin(9600);
}
void loop(){ int index[3]; String string[4];
index[0]=strIP.indexOf("."); index[1]=strIP.indexOf(".",index[0]+1); index[2]=strIP.indexOf(".",index[1]+1); string[0]=strIP.substring(0,index[0]); string[1]=strIP.substring(index[0]+1,index[1]); string[2]=strIP.substring(index[1]+1,index[2]); string[3]=strIP.substring(index[2]+1); Serial.println("String"); Serial.println(string[0]); Serial.println(string[1]); Serial.println(string[2]); Serial.println(string[3]); char* charIP1[5]; char* charIP2[3]; char* charIP3[3]; char* charIP4[3];
string[0].toCharArray(charIP1[0],string[0].length()+1); Serial.println("Start Char"); Serial.println(charIP1[0]); }
Thanks Steve
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #10 on: May 28, 2011, 02:42:47 pm » |
char* charIP1[5];
Now you've got an array of five pointers to "char", none of which are initialised. See reply #3.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 91
Posts: 9454
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #11 on: May 28, 2011, 02:48:01 pm » |
try this, byte ip[]={192,168,2,1}; String strIP="192.17.200.3";
void setup() { Serial.begin(115200); char buf[16]; strIP.toCharArray(buf,15); sscanf(buf, "%d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3]); for (int i=0; i<4; i++) Serial.println(ip[i],DEC);
} void loop(){}
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 11
Arduino rocks
|
 |
« Reply #12 on: May 28, 2011, 03:01:16 pm » |
Hi Awol, robtillaart,
Thanks for you patience Awol.
@robtillaart, your code works well, thank you for that. I am a little confused as to why I can use a char in your code but in mine it has to be a char*.
It must be because my string.toCharArray string is an array itself? i.e string[0].toCharArray
Not sure why this causes a problem.
Thanks
Steve
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Sr. Member
Karma: 0
Posts: 360
Arduino rocks
|
 |
« Reply #13 on: May 28, 2011, 03:33:42 pm » |
You're mixing up your toCharArray parameters: this: char* charIP1[5]; string[0].toCharArray(charIP1[0],string[0].length()+1);
should be this: char charIP1[5]; string[0].toCharArray(charIP1,string[0].length()+1);
Note the usage of charIP1 instead of charIP1[0]: charIP1 is a pointer to an array, charIP1[0] is the value of the first (zeroth) element in the array.
|
|
|
|
« Last Edit: May 28, 2011, 03:35:25 pm by Aeturnalus »
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 11
Arduino rocks
|
 |
« Reply #14 on: May 28, 2011, 03:41:00 pm » |
Hi Aeturnalus,
Thanks for this.
You are correct, that was the problem. I can now use char and not char* in the declaration.
Thanks
Steve
|
|
|
|
|
Logged
|
|
|
|
|
|