ip位址在數字與字串之間的相互轉換

2021-06-18 02:26:53 字數 726 閱讀 8692

#include

#include

#include

int inet_aton(const char *cp, struct in_addr *inp);

in_addr_t inet_addr(const char *cp);

in_addr_t inet_network(const char *cp);

char *inet_ntoa(struct in_addr in);

struct in_addr inet_makeaddr(int net, int host);

in_addr_t inet_lnaof(struct in_addr in);

in_addr_t inet_netof(struct in_addr in);

其中 inet_ntoa就可以完成我要的功能。簡單實現過程如下:

struct in_addr   stinaddr;//必要的結構體,只要包含前面三個標頭檔案,就不要犯愁這個結構的定義

unsigned int itemp;       //儲存整型的資料

unsigned char ipaddr[20]; //儲存轉換後的位址

stinaddr.s_addr=itemp;

pip=inet_ntoa(stinaddr);

strcpy(ipaddr,pip);

就可以把itemp轉換成字串儲存到ipaddr中。後面的工作就可以繼續下去了。

Java字串與陣列,字串與整型之間的相互轉換

tochararray 方法 語法 public char tochararray 例項 split 和split 方法 語法 public string split string regex,int limit 引數 regex 正規表示式分隔符。limit 分割的份數。例項 valueof 方法...

Go IP位址轉換 數字與字串之間高效轉換

ip 位址庫中 ip 位址的儲存格式一般有兩種,一種是點分十進位制形式 192.168.1.1 另一種是數字形式 3232235777 應用中,經常需要在這兩種格式之間做轉換。針對這乙個問題我在 exnet 擴充套件包裡面實現可兩者的轉換的快捷方法 使用示例 func main 每個位元組表示的範圍...

數字與字串之間的轉換

c語言為我們提供了數字和字串之間的轉換函式,這些函式有很多,常用的有 整型數轉字串函式itoa char itoa int value,char string,int radix int value 被轉換的整數,char string 轉換後儲存的字元陣列,int radix 轉換進製數,如2,8...