Go Down

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

StevenD

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

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy