Pages: 1 [2]   Go Down
Author Topic: String to Byte conversion.  (Read 966 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,

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:

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
Logged

Pages: 1 [2]   Go Up
Jump to: