QT 직렬 프로그램이 16진수로 데이터를 수락하고 발송했습니다

1800 단어 Qt
16진수로 데이터를 적용합니다.
4
//                 temp
    QByteArray temp = MyCom->readAll();
   
    QDataStream out(&temp,QIODevice::ReadWrite);    //       
    
    while(!out.atEnd())
    {
        qint8 outChar = 0;
        out>>outChar;   //       ,    
        //       
        QString str = QString("%1").arg(outChar&0xFF,2,16,QLatin1Char('0'));
        ui->textBrowser->insertPlainText(str);
    }
    
    ui->textBrowser->insertPlainText("
");
16진수로 데이터 전송:
    QString str = ui->lineEdit->text();// LineEdit     

    QByteArray senddata;

    StringToHex(str,senddata);// str      16     

    MyCom->write(senddata);//     

void Widget::StringToHex(QString str, QByteArray &senddata)
{
    int hexdata,lowhexdata;
    int hexdatalen = 0;
    int len = str.length();
    senddata.resize(len/2);
    char lstr,hstr;
    for(int i=0; i= len)
            break;
        lstr = str[i].toAscii();
        hexdata = ConvertHexChar(hstr);
        lowhexdata = ConvertHexChar(lstr);
        if((hexdata == 16) || (lowhexdata == 16))
            break;
        else
            hexdata = hexdata*16+lowhexdata;
        i++;
        senddata[hexdatalen] = (char)hexdata;
        hexdatalen++;
    }
    senddata.resize(hexdatalen);
}

char Widget::ConvertHexChar(char ch)
{
    if((ch >= '0') && (ch <= '9'))
            return ch-0x30;
        else if((ch >= 'A') && (ch <= 'F'))
            return ch-'A'+10;
        else if((ch >= 'a') && (ch <= 'f'))
            return ch-'a'+10;
        else return (-1);
}

좋은 웹페이지 즐겨찾기