Integer to IP Address - C

后端 未结 9 779
陌清茗
陌清茗 2020-12-07 17:45

I\'m preparing for a quiz, and I have a strong suspicion I may be tasked with implementing such a function. Basically, given an IP address in network notation, how can we ge

9条回答
  •  猫巷女王i
    2020-12-07 18:38

    #include "stdio.h"
    
    void print_ip(int ip) {
       unsigned char bytes[4];
       int i;
       for(i=0; i<4; i++) {
          bytes[i] = (ip >> i*8) & 0xFF;
       }
       printf("%d.%d.%d.%d\n", bytes[3], bytes[2], bytes[1], bytes[0]);
    }
    
    int main() {
       int ip = 0xDEADBEEF;
       print_ip(ip);   
    }
    

提交回复
热议问题