【类型转换】string 转成 int解决办法
【类型转换】string 转成 int
收到一个存有ip的指针,我放到了string里面。
例如:
我如何实现取出121并转换成int型,
------解决方案--------------------
------解决方案--------------------
sscanf(s_ip,"%d.%d.%d.%d",&a,&b,&c,&d )
------解决方案--------------------
收到一个存有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