C++中输出16进制数
文章目录
方法1:
char data[2]={0x0A,0x0B};
char result[8];
for(int i=0; i<2; i++)
{
sprintf(result,"%X",data[i]); // %X结果为大写,%x结果为小写 cout<<"result = "<<result[i]<<endl; }
方法2:
// #include <iomanip>
char data[2]={0x0A,0x0B};
for(int i=0; i<2; i++)
{
cout<<"result = "<<hex << int(data[i])<<endl;
}
使用cout输出时,hex控制符只对整数有效,所以需要先将16进制转成整数,再输出。
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论