Advertisement
AntonioVillanueva

Conversion d'une chaîne ip en ip en tableau de bytes

Apr 13th, 2022
1,408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. /*Antonio Villanueva Segura
  3.  * Test Conversion d'une chaîne ip en ip en mode tableau d'octets
  4.  */
  5. void StringIpToByteIp(String ips ,byte *ip, int size){
  6.     String tmp="";
  7.     for (int i=0; i< ips.length()+1 ;i++){
  8.        if ((ips[i])=='.' || ips[i]==0){ //A atteint le point de séparation ... ou la fin
  9.         *ip=tmp.toInt();// Enregistre la chaîne en tant qu'octet dans le tableau ip
  10.         ip++; //avancer d'une position dans l'array ip de type byte  
  11.         tmp="";//Clear  
  12.        }else {tmp +=(ips[i]);}//avancer d'une position dans le String      
  13.     }
  14. }
  15.  
  16. //Conversion ip byte array to String
  17. String ByteIpToString(byte *ip, int size){
  18.   String tmp="";
  19.   while (size --){
  20.     tmp+= String( *(ip++) );
  21.     tmp+= char ((size>0) ? '.': 0);//S'il s'agit du dernier chiffre,  pas de point.
  22.   }
  23.   return tmp;
  24. }
  25.  
  26. //Affiche via le port série le contenu d'un tableau d'octets
  27. void printArray(byte *ip,int size){
  28.   while (size --){
  29.     Serial.println (*ip);
  30.     ip++;
  31.   }  
  32. }
  33.  
  34. void setup() {
  35.   Serial.begin(9600);
  36.  
  37.   String ips = "192.168.6.91";//String ip
  38.   byte ip[4];//Array bytes
  39.   StringIpToByteIp( ips ,ip, 4);//Convertir le String en un tableau de bytes byte [4]={ip1,ip2.....}
  40.   printArray (ip,4);//Affiche via le port série le contenu d'un tableau d'octets
  41.   Serial.print ("String = ");
  42.   Serial.println (ByteIpToString(ip,4));
  43.   while (true){}
  44. }
  45.  
  46.  
  47. void loop() {
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement