Pages: [1] 2   Go Down
Author Topic: String to Byte conversion.  (Read 1027 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Brattain Member
*****
Karma: 630
Posts: 50009
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Paul,

Thanks for your reply.

It is the toCharArray that I am having problems with.

Here is the sketch.
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];
  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 Offline
Brattain Member
*****
Karma: 308
Posts: 26455
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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".

Code:
char charIP1[4];
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Awol,

I thought I had to use char*, so that I could use atoi later on?

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Brattain Member
*****
Karma: 308
Posts: 26455
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Show your code.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Awol,

The code is the same as posted above.

Thanks

Steve
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26455
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You mean this?
Code:
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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Awol,

Here is the code.
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 Offline
Brattain Member
*****
Karma: 308
Posts: 26455
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 220
Posts: 13834
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

try this,

Code:
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

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

0
Offline Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Sr. Member
****
Karma: 0
Posts: 360
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You're mixing up your toCharArray parameters:

this:
Code:
char* charIP1[5];
 string[0].toCharArray(charIP1[0],string[0].length()+1);
should be this:
Code:
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 Offline
Newbie
*
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1] 2   Go Up
Jump to: