C++ 将IP地址172.17.72.250 保存到4字节存储区域,再转化成字符串,打印出无符号整数值

编写C++程序。求高手指导,急啊。万分感谢。
2025-05-16 23:08:53
推荐回答(5个)
回答1:

#include
int main(void)
{
char str[]="172.17.72.250";
unsigned char ip[4]={0,0,0,0};
sscanf(str, "%d.%d.%d.%d", ip, ip+1, ip+2, ip+3);
printf("%02x %02x %02x %02x \n", ip[0], ip[1], ip[2], ip

[3]);
return 0;
}
/*运行结果:
ac 11 48 fa
*/

回答2:

有系统函数可以将字符串的IP地址转换为DWORD,从DWORD类型转换为4个字节型的还难吗?

回答3:

so fucking the boys of computer class

#include
#include
using namespace std;
int main() {
unsigned iIP = 0, iIn;
string strIn;
/*读入IP*/
cin >> strIn;
/*将IP转成Unsigned*/
stringstream ss( strIn );
for( int i = 3 ; i >= 0 ; --i ) {
ss >> iIn;
iIP |= iIn << ( i * 8 );
ss.get();
}
cout << iIP << endl;
/*将Unsigned转成字符串*/
string strOut = "";
for( int i = 3 ; i >=0 ; --i ) {
stringstream ssTemp;
ssTemp << ((iIP >> (i*8) ) & 0xff);
ssTemp >> strIn;
strOut += strIn;
strOut += i ? ".":"";
}
cout << strOut;

while(1);
return 0;
}

回答4:

信曾哥....不挂科.........
凑热闹着...CK哥....
我肯定说曾哥不会来.....但要防止春哥来........
得到毛....做唔出听日就死硬..........

回答5:

欢老板,我同你一齐死!