char[]转string的问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了char[]转string的问题相关的知识,希望对你有一定的参考价值。
string URLEncode(const char *sIn,UINT nLen) string sOut=""; unsigned char*buf = new unsigned char[nLen*4]; memset( buf, 0, nLen*4); for( size_t ix = 0, n = 0; ix < nLen; ix++ ,n+=4) if( isalnum( (unsigned char)sIn[ix] ) ) buf[n] = sIn[ix]; else buf[n] = '%'; buf[n+1] = toHex( (unsigned char)sIn[ix] >> 4 ); buf[n+2] = toHex( (unsigned char)sIn[ix] % 16); sOut += (char *)buf; delete []buf; return sOut;;>50msstring URLEncode(const char *sIn,int nLen) string sOut; for( size_t ix = 0; ix < nLen; ix++ ) unsigned char buf[4]; if( isalnum( (unsigned char)sIn[ix] ) ) buf[0] = sIn[ix]; buf[1] = 0; buf[2] = 0; buf[3] = 0; else buf[0] = '%'; buf[1] = toHex( (unsigned char)sIn[ix] >> 4 ); buf[2] = toHex( (unsigned char)sIn[ix] % 16); buf[3] = 0; sOut += (char *)buf; return sOut;;>120ms有这两段代码,代码1的运行效率比较高,但是显示的结果有问题,而且重复执行会报错,代码2不会报错但是效率比较低,怎么能使代码1稳定呢?
string URLEncode(const char *sIn,UINT nLen)
string sOut="";
unsigned char*buf = new unsigned char[nLen*4];
memset( buf, 0, nLen*4);
for( size_t ix = 0, n = 0; ix < nLen; ix++ ,n+=4)
if( isalnum( (unsigned char)sIn[ix] ) )
buf[n] = sIn[ix];
else
buf[n] = '%';
buf[n+1] = toHex( (unsigned char)sIn[ix] >> 4 );
buf[n+2] = toHex( (unsigned char)sIn[ix] % 16);
sOut += (char *)buf;
delete []buf;
return sOut;
;
>50ms
string URLEncode(const char *sIn,UINT nLen,string &sOut)//sOut应作为形参带过来
//string sOut="";//这个声明要在主调函数中,否则函数退出后就消失了,犯了C/C++之大忌
char *buf=new char[nLen*3+1];//直接声明为char*型,强制转换也要时间
//memset(buf,0,nLen*4);//本句没有必要了
for(size_t ix=0,n=0;ix<nLen;ix++)
if(isalnum((char)sIn[ix]))
buf[n++]=sIn[ix];
else
buf[n++] = '%';
buf[n++] = toHex((char)sIn[ix] >> 4 );
buf[n++] = toHex((char)sIn[ix] % 16);
buf[n]='\0';//增加一句置字符串结束符
sOut += buf;
delete []buf;
return sOut;
追问
你再想想吧
追答让我想什么?用下面的代码模拟,很正常。
int toHex(char ch)
return ch>'9' ? ch+0x37 : ch+'0';
string URLEncode(const char *sIn,int nLen,string &sOut)
//string sOut="";
char *buf=new char[nLen*3+1];
//memset(buf,0,nLen*3);
for(size_t ix=0,n=0;ix> 4);
buf[n++] = toHex((char)sIn[ix] % 16);
buf[n]='\0';
sOut += buf;
delete []buf;
return sOut;
void main(void)
string str="";
const char a[]="aj3l6k4lj3?gk1=3ji8f-v3";
cout << a << endl << URLEncode(a,strlen(a),str) << endl;
我会告诉你,我Func();while(1);的前5~6次也是正常的,到20+,50+的时候才崩掉么
追答调试代码不能几次正常就认为正确,特别是边缘条件处。我已指出你的代码有两个问题:其中关于string sOut;的问题我的理解有误——你这里是对象而不是对象指针,所以在函数中声明是可以的。二是处理后的buf中有很多为0的字节,sOut += (char *)buf;后只能得到第一个0以前的字串,比如处理后的buf[]="123000yx59000",那么sOut只可能是123,而不会是123000yx59000……你完全可以将我的意见向你的老师或其他高手证实。祝走运!
参考技术B public class Testpublic static void main(String args[])
char[] chs = 'a','f','d';
String chToStr = String.valueOf(chs);
System.out.println(chToStr);
String str = "fdssf";
chs = str.toCharArray();
for(int a = 0; a < chs.length; a ++)
System.out.println(chs[a]);
这样可以么?
剑指offer知识点List转int[],List转String,String转int,char[]转String,String 转char[],List转String[]
[1] List转int[]
List<Integer> list=new ArrayList<Integer>();
list.stream().mapToInt(Integer::intValue).toArray(); // 方法一
list.stream().mapToInt(i->i).toArray(); // 方法二
[2] List转String
List<String> l = new ArrayList<>();
l.stream().map(String::valueOf).collect(Collectors.joining(",")); // 方法一
String test = String.join(",",l); // 方法二
[3] String转int
Integer.parseInt(“100”);
[4] char[]转String
String s;
Char[] arr = s.toCharArray()
[5] String 转char[]
String s = String.valueOf(arr)
[6] List转String[]
list.toArray(new String[list.size()])
以上是关于char[]转string的问题的主要内容,如果未能解决你的问题,请参考以下文章