[solved] Dumping values in Vector Buffer



  • I am new to Qt , currently developing an application which will parse a XML file and dump the values in the vector Buffer .

    my XML file look like this

    <Root>
    <class name="ADC"      type="DWORD"  value="0xDEEDBEAF" />
    <class name="BDC"      type="WORD"   value="0xEFCDEG" />
    <class name="MDC"      type="BYTE"   value="0xEFCDEG" />
    </Root>
    

    we have one (uchar) vector (buffer) . where the values of the XML attributes needs to be dumped.

    vector<uchar> buf;
    The problem is that when we will get the value(0xDEEDBEAF,0xEFCDEG ...) of each attribute tag , QString is returned

    QString value = classElement.attribute("value"); //Value is of type QString.
    But i need to push the data in to (uchar) vector according to BYTE , WORD, DWORD .

    i researched in the internet , but i am not able to get any good source of converting QString to BYTE , QString to WORD , QString to DWORD.

    Any help to convert Qstring to BYTE,WORD,DWORD will be great help full. So that data can be pushed into uchar vector.

    Thanks

    Edit: Added code tags -- @Wieland


  • Moderators

    @akshay123 After writing everything into this buffer, how do you know how to read it? I mean, how do you know whether you need to read DWORD, WORD or BYTE?
    Your buffer will look like: [DE, ED, BE, AF, EF, CD, EG, EF, CD, EG]
    Note: G is not valid hex number!
    Why not use QByteArray?
    To convert hex string to number use http://doc.qt.io/qt-5/qstring.html#toInt



  • After writing on to buffer, We have another xml file which keeps tracks of indices , With the help of those indices we are going to read which is next element (WORD,DWORD or BYTE ) to be read . Yes G is not a valid Hex number , While posting there was a typo mistake .



  • In addition to what previous ans look at the documentation of qstring.There are so many functions like toascii() tolatin() tolocal8bit() etc. They should help you.



  • @jsulm Thanks for your sugestion ,

    Currently i have values like this

    value="10"
    value="11"
    value="12"
    value="13"
    value="14"
    value="15"
    value="16"
    value="07"

    Those are in decimal . But when i tried to convert these to hex using Qstring.setnum( ). I am not able to get the hex values .

    Thye code sniped it like this

    QString value_SB = tempElement.attribute("value");
    bool ok1;
    int value_B1= value_SB.toInt(&ok1, 16);
    QString hex_value;
    hex_value.setNum(value_B1,16);
    int result1 = hex_value.toInt(&ok1, 16);
    buff.push_back((uchar)result1);

    But the output is same 10 11 12 13


  • Moderators

    @akshay123 Why do you want to convert to hex? There is no need for that.


  • Moderators

    @akshay123 Just do toInt() as it is decimal



  • @jsulm some numbers are in decimal format and some are hexadecimal .

    I want all the numbers whould be in hexadecimal format


  • Moderators

    @akshay123 Then you need to know which number is in which format: decimal 10 is not the same as 10 in hex.



  • bool valueInVector(QString val,vector<uchar>& buf){
    int base=10;
    if(val.leftRef(2).compare("0x",QString::CaseInsensitive)==0){
    base=16;
    val.remove(0,2);
    }
    bool check=true;
    for(int i=0;i<val.size() && check;i+=2){
    const uchar tempVal=val.midRef(i,2).toUInt(&check,base);
    if(check)
    buf.push_back(tempVal);
    }
    return check;
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.