inst是int型变量,值是0x23baffec
然后开始调试的时候发现:
这一步bicode是对的,分别是0x23 0xba 0xff 0xec
到下一步
tmp就不对了,还少了4bit,这是为什么?
哪位大神可以给我解释一下这个问题?
或者说直接告诉我怎么把int二进制写入到TextEdit里面去也可以
拜托拜托,请帮帮我
2 个解决方案
#1
int i = 0x23baffec;
QString str;
str += (unsigned char) (i >> 24) & 0xff;
str += (unsigned char) (i >> 16) & 0xff;
str += (unsigned char) (i >> 8) & 0xff;
str += (unsigned char) i & 0xff;
#2
189对应的应该是0XBD,也不应该是0XBA吧。另外,不要使用char,char一般针对字符,负数会导致转换失败。
int inst = 0x23BDffEC;
unsigned char bicode[4]={0};
bicode[3] = (unsigned char)(inst&0xFF);
bicode[2] = (unsigned char)(inst>>8)&0xFF;
bicode[1] = (unsigned char)(inst>>16)&0xFF;
bicode[0] = (unsigned char)(inst>>24)&0xFF;
QString tmp;
for(int i=0;i<4;i++)
{
tmp+=QString::number(bicode[i],16);//调用这个函数即可
}
ui->textEdit->insertPlainText(tmp);
int inst = 0x23BDffEC;
unsigned char bicode[4]={0};
bicode[3] = (unsigned char)(inst&0xFF);
bicode[2] = (unsigned char)(inst>>8)&0xFF;
bicode[1] = (unsigned char)(inst>>16)&0xFF;
bicode[0] = (unsigned char)(inst>>24)&0xFF;
QString tmp;
for(int i=0;i<4;i++)
{
tmp+=QString::number(bicode[i],16);//调用这个函数即可
}
ui->textEdit->insertPlainText(tmp);
#1
int i = 0x23baffec;
QString str;
str += (unsigned char) (i >> 24) & 0xff;
str += (unsigned char) (i >> 16) & 0xff;
str += (unsigned char) (i >> 8) & 0xff;
str += (unsigned char) i & 0xff;
#2
189对应的应该是0XBD,也不应该是0XBA吧。另外,不要使用char,char一般针对字符,负数会导致转换失败。
int inst = 0x23BDffEC;
unsigned char bicode[4]={0};
bicode[3] = (unsigned char)(inst&0xFF);
bicode[2] = (unsigned char)(inst>>8)&0xFF;
bicode[1] = (unsigned char)(inst>>16)&0xFF;
bicode[0] = (unsigned char)(inst>>24)&0xFF;
QString tmp;
for(int i=0;i<4;i++)
{
tmp+=QString::number(bicode[i],16);//调用这个函数即可
}
ui->textEdit->insertPlainText(tmp);
int inst = 0x23BDffEC;
unsigned char bicode[4]={0};
bicode[3] = (unsigned char)(inst&0xFF);
bicode[2] = (unsigned char)(inst>>8)&0xFF;
bicode[1] = (unsigned char)(inst>>16)&0xFF;
bicode[0] = (unsigned char)(inst>>24)&0xFF;
QString tmp;
for(int i=0;i<4;i++)
{
tmp+=QString::number(bicode[i],16);//调用这个函数即可
}
ui->textEdit->insertPlainText(tmp);