C++整數和IP字符串轉換
char* UlongToIp(ULONG Num) { //Num = 52406464; //return x.x.x.x char *IP = NULL; unsigned int ip_int_index[4], ip_temp_numbr = 24; int i = 0; for(i = 0; i < 4; i++) { ip_int_index[i]=(Num>>ip_temp_numbr)&0xFF; ip_temp_numbr-=8; } if ((IP=(char *)malloc(17*sizeof(char)))==NULL) { return "ERROR"; } sprintf(IP,"%d.%d.%d.%d",ip_int_index[0],ip_int_index[1],ip_int_index[2],ip_int_index[3]); return IP; }
參考: C语言IP地址与int互转