Go Down

Topic: String to Byte conversion. (Read 1 time) previous topic - next topic

StevenD

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

PaulS

#1
May 28, 2011, 06:12 pm Last Edit: May 28, 2011, 06:15 pm by PaulS Reason: 1
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.

StevenD

Hi Paul,

Thanks for your reply.

It is the toCharArray that I am having problems with.

Here is the sketch.
Code: [Select]


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

AWOL

Code: [Select]
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: [Select]
char charIP1[4];
"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.

StevenD

Thanks Awol,

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


StevenD

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

AWOL

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

StevenD

Hi Awol,

The code is the same as posted above.

Thanks

Steve

AWOL

You mean this?
Code: [Select]
char* charIP1[3];
  char* charIP2[3];
  char* charIP3[3];
  char* charIP4[3];


Post your code.
"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.

StevenD

Hi Awol,

Here is the code.
Code: [Select]


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

AWOL

Code: [Select]
char* charIP1[5];

Now you've got an array of five pointers to "char", none of which are initialised.
See reply #3.
"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.

robtillaart

try this,

Code: [Select]

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(){}
Rob Tillaart

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

StevenD

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

Aeturnalus

#13
May 28, 2011, 10:33 pm Last Edit: May 28, 2011, 10:35 pm by Aeturnalus Reason: 1
You're mixing up your toCharArray parameters:

this:
Code: [Select]

char* charIP1[5];
string[0].toCharArray(charIP1[0],string[0].length()+1);

should be this:
Code: [Select]

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.  

StevenD

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

Go Up