adafruit/Adafruit_CC3000_Library

ip string to ipu32 helper function

dzzie opened this issue · 1 comments

a nice to have helper func:

//ip string to ipu32 -  usage:  uint32_t ip = ips2ip("192.168.0.10");
uint32_t ips2ip(char* ips){
  int i=0, j=0;
  uint8_t b[4]={0,0,0,0};
  char tmp[5]={0,0,0,0,0};

  while(*ips){
      if(*ips=='.'){
          b[j++] = atoi(tmp);
          for(; i>=0; i--) tmp[i]=0; //wipe tmp for next
          i=0;
          if(j == 4) break; //max 4 sections
      }else{
         tmp[i++] = *ips;
         if( i > 4 ) break; //max 3 chars per sect
      }
      ips++;
  }

  if(tmp[0] != 0) b[j++] = atoi(tmp);
  if(j != 4) return 0; //need exactly 4 sections
  return cc3000.IP2U32(b[0], b[1], b[2], b[3]);

}

Oh nice, thanks for contributing that function! We've had a few issues with the code size of the library growing over time so I think it's best to keep this outside the library for now and just use the IP2U32 function. However it's great that this is here in an issue that folks can find if searching for a string to IP function they want to add to their sketch. Thanks again for contributing!