Integer to IP Address - C

后端 未结 9 766
陌清茗
陌清茗 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条回答
  •  不思量自难忘°
    2020-12-07 18:44

    Hint: break up the 32-bit integer to 4 8-bit integers, and print them out.

    Something along the lines of this (not compiled, YMMV):

    int i = 0xDEADBEEF; // some 32-bit integer
    printf("%i.%i.%i.%i",
              (i >> 24) & 0xFF,
              (i >> 16) & 0xFF,
              (i >> 8) & 0xFF,
              i & 0xFF);
    

提交回复
热议问题