String to Byte conversion.

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

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.

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

 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];

Thanks Awol,

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

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

Show your code.

Hi Awol,

The code is the same as posted above.

Thanks

Steve

You mean this?

char* charIP1[3];
  char* charIP2[3];
  char* charIP3[3];
  char* charIP4[3];

Post your code.

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

char* charIP1[5];

Now you've got an array of five pointers to "char", none of which are initialised.
See reply #3.

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

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

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.

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

Hi All,

Thanks to everyone, who helped me along with this one.

He is the full code, in case anyone else stumbles across this problem.

Obviously you can remove the Serial.println lines, these were for debugging to ensure I get the same characters along the way.

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[5];
  char charIP3[5];
  char charIP4[5];

  string[0].toCharArray(charIP1,4);
  string[1].toCharArray(charIP2,4);
  string[2].toCharArray(charIP3,4);
  string[3].toCharArray(charIP4,4);


  Serial.println("Start Char");
  Serial.println(charIP1);
  Serial.println(charIP2);
  Serial.println(charIP3);
  Serial.println(charIP4);

  int intIP1;
  int intIP2;
  int intIP3;
  int intIP4;

  intIP1=atoi(charIP1);
  intIP2=atoi(charIP2);
  intIP3=atoi(charIP3);
  intIP4=atoi(charIP4);

  Serial.println("Start Int");
  Serial.println(intIP1);
  Serial.println(intIP2);
  Serial.println(intIP3);
  Serial.println(intIP4);

  byte newIP[]={intIP1,intIP2,intIP3,intIP4};

  Serial.println("Bytes");
  Serial.println(newIP[0],DEC);
  Serial.println(newIP[1],DEC);
  Serial.println(newIP[2],DEC);
  Serial.println(newIP[3],DEC);
}

Thanks Again

Steve