string和c string的问题

pinkfIoyd 2014-03-10 07:11:11
有一个问题是这样的:
原理:ip地址的每段可以看成是一个0-255的整数,把每段拆分成一个二进制形式组合起来,然后把这个二进制数转变成
一个长整数。
举例:一个ip地址为10.0.3.193
每段数字 相对应的二进制数
10 00001010
0 00000000
3 00000011
193 11000001
组合起来即为:00001010 00000000 00000011 11000001,转换为10进制数就是:167773121,即该IP地址转换后的数字就是它了。
每段可以看成是一个0-255的整数,需要对IP地址进行校验

题目大概就是这个样子

我写的代码如下:


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
using namespace std;
/* 功能:将输入的string类型的IP信息转换为string类型
* 输入:string类型的IP信息
* 输出:DWORD结果,正常返回解析结果值,异常时,dwIP为0
* 返回:返回解析的整型,异常时返回"0"
*/
string GetValueByIP(string strIP)
{
int index1,index2,index3,index4;
int seg1,seg2,seg3,seg4;
unsigned int ires = 0x0;
string segstr1,segstr2,segstr3,segstr4,res = "0";

if ( ((index1 = strIP.find('.')) == -1) ||
((index2 = strIP.find('.',index1 + 1)) == -1) ||
((index3 = strIP.find('.',index2 + 1)) == -1) ||
((index4 = strIP.find('.',index3 + 1)) != -1))
{
return res;
}

segstr1 = strIP.substr(0,index1);
segstr2 = strIP.substr(index1+1,index2-index1-1);
segstr3 = strIP.substr(index2+1,index3-index2-1);
segstr4 = strIP.substr(index3+1,segstr4.length()-index3-1);

seg1 = atoi(segstr1.c_str());
seg2 = atoi(segstr2.c_str());
seg3 = atoi(segstr3.c_str());
seg4 = atoi(segstr4.c_str());

if ( ((seg1 < 0) || (seg1 > 255)) ||
((seg2 < 0) || (seg2 > 255)) ||
((seg3 < 0) || (seg3 > 255)) ||
((seg4 < 0) || (seg4 > 255)))
{
return res;
}

ires = ((unsigned int)(seg1&0xff)<<24) |
((unsigned int)(seg2&0xff)<<16) |
((unsigned int)(seg3&0xff)<<8) |
((unsigned int)(seg4&0xff));

itoa(ires,(char*)res.c_str(),10);

return res;
}
/* 功能:将输入的整数IP信息转换为string型IP信息,即X.X.X.X的格式
* 输入:string类型的值
* 输出:X.X.X.X的格式的IP信息
* 返回:正常解析时返回非空的IP信息,异常时strIP为空值
*/
string GetIPByValue(string strValue)
{
string res = "";
unsigned int input;
int seg1,seg2,seg3,seg4;
input = atoi(strValue.c_str());
if (input < 0)
{
return res;
}

seg1 = (input & 0xff000000) >> 24;
seg2 = (input & 0x00ff0000) >> 16;
seg3 = (input & 0x0000ff00) >> 8;
seg4 = input & 0x000000ff;
sprintf((char*)res.c_str(),"%d.%d.%d.%d",seg1,seg2,seg3,seg4);
return res;
}
int main(void)
{
string res1,res2;
res1 = GetIPByValue("2131393279");
cout << res1 << endl;
res2 = GetValueByIP("127.10.122.255");
cout << res2 << endl;
return 0;
}



问题是:
在两个子函数中执行结果都是对的?为什么打印出来的时候就出错了?应该怎么写?求高手指点一二!
附加两个问题:
1)整型如何转换成c++里的string?
2)c中的char*如何转换成c++中的string?
...全文
109 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lm_whales 2014-03-12
  • 打赏
  • 举报
回复
sprintf((char*)res.c_str(),"%d.%d.%d.%d",seg1,seg2,seg3,seg4); 是明显有问题的 不能保证结果是正确的
buyong 2014-03-11
  • 打赏
  • 举报
回复
sprintf
赵4老师 2014-03-11
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
int main() {
    int i,v;
    char bs[33];
    char b[33];
    char hs[9];
    char h[9];
    char s[4];
    char *e;

// 十进制整数转二进制串;
    i=1024;
    ltoa(i,b,2);
    sprintf(bs,"%032s",b);
    printf("i=%d,bs=%s\n",i,bs);
// 十进制整数转十六进制串;
    i=1024;
    ltoa(i,h,16);
    sprintf(hs,"%08s",h);
    printf("i=%d,hs=%s\n",i,hs);
// 十六进制字符串转成十进制数
    strcpy(hs,"00000400");
    sscanf(hs,"%x",&i);
    printf("hs=%s,i=%d\n",hs,i);
// 二进制字符串转化为十六进制字符串;
    strcpy(bs,"00000000000000000000010000000000");
    i=strtol(bs,&e,2);
    ltoa(i,h,16);
    sprintf(hs,"%08s",h);
    printf("bs=%s,hs=%s\n",bs,hs);
// 二进制字符串转化为十进制数;
    strcpy(bs,"00000000000000000000010000000000");
    i=strtol(bs,&e,2);
    printf("bs=%s,i=%d\n",bs,i);
// 十六进制字符串转成二进制串
    strcpy(hs,"00000400");
    sscanf(hs,"%x",&i);
    ltoa(i,b,2);
    sprintf(bs,"%032s",b);
    printf("hs=%s,bs=%s\n",hs,bs);
// ASC\GBK字符串转十六进制串
    strcpy(s,"a汉");
    i=0;
    while (1) {
        if (0==s[i]) break;
        sprintf(hs+i*2,"%02X",(unsigned char)s[i]);
        i++;
    }
    setlocale(LC_ALL,"chs");
    printf("s=%s,hs=%s\n",s,hs);
// 十六进制字符串转成汉字(GBK)及字符(ASC)
    strcpy(hs,"61BABA");
    i=0;
    while (1) {
        if (1!=sscanf(hs+i*2,"%2x",&v)) break;
        s[i]=(char)v;
        i++;
    }
    s[i]=0;
    printf("hs=%s,s=%s\n",hs,s);

    return 0;

}
//i=1024,bs=00000000000000000000010000000000
//i=1024,hs=00000400
//hs=00000400,i=1024
//bs=00000000000000000000010000000000,hs=00000400
//bs=00000000000000000000010000000000,i=1024
//hs=00000400,bs=00000000000000000000010000000000
//s=a汉,hs=61BABA
//hs=61BABA,s=a汉
修巴利耶 2014-03-10
  • 打赏
  • 举报
回复
1)atoi 2)string的构造函数string (const char* s);

64,653

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧