C语言:如何将无符号char数组转为字符串
unsignedcharsrc[6]={0x12,0x32,0x56,0x78,0x90,0xab},如何转为:char[12]={"1232567890ab"}?非常感...
unsigned char src[6] = {0x12, 0x32,0x56,0x78,0x90,0xab},如何转为:
char[12] = {"1232567890ab"}?
非常感谢! 展开
char[12] = {"1232567890ab"}?
非常感谢! 展开
2个回答
推荐于2017-11-27
展开全部
int main()
{
unsigned char src[6] = {0x12, 0x32,0x56,0x78,0x90,0xab};
char buffer[20];//维数定义大些
for(int i=0;i<6;i++)
sprintf( buffer+i*2,"%x",src[i]);//格式化输出到buffer,每个unsigned char 转换为字符后占两个位置,%x小写输出,%X大写输出
printf("%s\n",buffer);
return 0;
}
{
unsigned char src[6] = {0x12, 0x32,0x56,0x78,0x90,0xab};
char buffer[20];//维数定义大些
for(int i=0;i<6;i++)
sprintf( buffer+i*2,"%x",src[i]);//格式化输出到buffer,每个unsigned char 转换为字符后占两个位置,%x小写输出,%X大写输出
printf("%s\n",buffer);
return 0;
}
追问
非常感谢,学习了!
可否再请教下,如果要转成“2121658709ba”,有没有比较方便的方法?
追答
unsigned char src[6] = {0x12, 0x32,0x56,0x78,0x90,0xab};
char buffer[20];//维数定义大些
char buffer1[20]={'\0'};
for(int i=0;i<6;i++)
{
sprintf( buffer+i*2,"%x",src[i]);//格式化输出到buffer,每个unsigned char 转换为字符后占两个位置,%x小写输出,%X大写输出
buffer1[i*2]=*(buffer+i*2+1);//处理字符,倒序
buffer1[i*2+1]=*(buffer+i*2);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询