Important: Please read the Qt Code of Conduct -

[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

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

    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.


    Edit: Added code tags -- @Wieland

  • Qt Champions 2019

    @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

  • 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 .

  • Qt Champions 2017

    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


    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;
    int result1 = hex_value.toInt(&ok1, 16);

    But the output is same 10 11 12 13

  • Qt Champions 2019

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

  • Qt Champions 2019

    @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

  • Qt Champions 2019

    @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;
    bool check=true;
    for(int i=0;i<val.size() && check;i+=2){
    const uchar tempVal=val.midRef(i,2).toUInt(&check,base);
    return check;

Log in to reply