码迷,mamicode.com
首页 > 编程语言 > 详细

串口发送Hex数组

时间:2015-01-17 16:26:06      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:

void MainWindow::String2Hex(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; )
    {
        //char lstr,
        hstr=str[i].toLatin1();
        if(hstr ==  )
        {
            i++;
            continue;
        }
        i++;
        if(i >= len)
            break;
        lstr = str[i].toLatin1();
        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 MainWindow::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);
}

 

串口发送Hex数组

标签:

原文地址:http://www.cnblogs.com/CoderTian/p/4230506.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!