现有CString类型和int类型要转化成16进制(0x+十六进制数)形式,赋值给char buff[],
要求显示的值不变,例如:
CString str="8050131298"赋值后buff={0x80,0x50,0x13,0x12,0x98}
int i=32 --> buff={0x32}
int i=2 --> buff={0x02}
各位大虾请赐教!小弟在此先谢过了!
要求显示的值不变,例如:
CString str="8050131298"赋值后buff={0x80,0x50,0x13,0x12,0x98}
int i=32 --> buff={0x32}
int i=2 --> buff={0x02}
各位大虾请赐教!小弟在此先谢过了!
int i=2 --> buff={0x02}buff[0]=i;
char buff[12]={0};
int i;
int len=strlen(temp);
for (i=0;i<len;i++)
{
buff[i/2]<<=4;
buff[i/2]|=temp[i+2]<'A'?temp[i+2]-'0':temp[i+2]-'A'+10;
}
不过这种方法没有错误处理,只是演示一下。
char temp[]="8050131298";
char buff[12]={0};
int i;
int len=strlen(temp);
for (i=0;i<len;i+=2)
{
buff[i/2]=temp[i]<'A'?temp[i]-'0':temp[i]-'A'+10;
buff[i/2]<<=4;
buff[i/2]|=temp[i+1]<'A'?temp[i+1]-'0':temp[i+1]-'A'+10;
}