QT查找字符串中的内容
例子:
例如查找字符串中的16进制数据0Xxx。
unsigned char func( unsigned char c){
static unsigned char sta[16] ={//不多不少的静态数据
0x00,0x08,0x04,0x0C,0x02,0x0A,0x06,0x0E,0x01,0x09,0x05,0x0D,0x03,0x0B,0x07,0x0F
};
unsigned char d = 0; //没有判断
d |= (sta[c&0xF]) << 4;
d |= sta[c>>4];
return d;
}
QString str = ui->textEdit->toPlainText();
// 正则表达式
QRegExp rx(“0x([0-9a-fA-F]+)”);
int pos=0;
while(1)
{
//符合正则表达式每一个的偏移位置
pos = str.indexOf(rx,pos);
//qDebug()<<pos;
if ( pos >= 0 )
{
QString num = rx.cap(0);
num.remove(0,2);
//字符串转换整数
unsigned char byte = num.toInt(NULL,16);
unsigned char res = func(byte);
QString a;
if(res<16)
{
//整数转换字符串
a = “0x0”+QString::number(res,16);
}else
{
a =“0x”+QString::number(res,16);
}
str.replace(pos,4,a);
pos+=4;
} else
{
break;
}
ui->textEdit_2->clear();
ui->textEdit_2->append(str);
}