首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

【类型转换】string 转成 int解决办法

2013-11-14 
【类型转换】string 转成 int收到一个存有ip的指针,我放到了string里面。例如:char s_ip[16]char* p s_ip

【类型转换】string 转成 int
收到一个存有ip的指针,我放到了string里面。
例如:


char s_ip[16];
char* p = s_ip;
string ip;
string::iterator it_ip;

memcpy(s_ip, _tip+1, _tip->abtLen-sizeof(TIP_HEAD));//tip包中为ip,写给s_ip
ip = s_ip;//此时我想检索出ip的最后一位
//e.g. 192.163.1.121  取出121转换成int型 

我如何实现取出121并转换成int型,
[解决办法]
std::string ip ="192.163.1.121";
std::stringstream s(ip);
int a,b,c,d; //to store the 4 ints
char ch; //to temporarily store the '.'
s >> a >> ch >> b >> ch >> c >> ch >> d;
std::cout << a << "  " << b << "  " << c << "  "<< d;

[解决办法]
sscanf(s_ip,"%d.%d.%d.%d",&a,&b,&c,&d )
[解决办法]


int a[4] = {0}
sscanf(s_ip, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);

//a[0] = 192 
//a[1] = 163 
//a[2] = 1 
//a[3] = 121

热点排行