Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Assign unsigned char data to QByteArray



  • Hi all,

    I spent almost two days looking at discussion and various links on google, but I cannot solve my problem.

    I have an array like this: "unsigned char c[8];". It's inserted into a structure called "rcv_message".
    The array correctly filled with integer values.....so if I try to print it as soon as I receive data, I got this output:

    char data 0
    char data 25
    char data 0
    char data 0
    char data 0
    char data 255
    char data 154
    char data 0

    and this absolutly correct. Now, I want to put this values into a QByteArray, and to do so I tried two ways:
    //first one:
    QByteArray canPayload;
    canPayload.setRawData((const char*)rcv_message.data.c,8);

    //second one
    for(int i = 0; i<8; i++)
    canPayload[i] = rcv_message.data.c[i]; //(or: canPayload.append(rcv_message.data.c[i])

    This two methods are resulting both in:
    "\x00\x19\x00\x00\x00\xFF\x9A\x00"

    which is correct and it's exactly want I want. Now the problem is that if I try to change some values from source, the values are converted in ASCII. So if I try to change the third byte from "00" to "50" (exadecimal), in output I got "P" at third byte position. I would like to keep the value: "\x50" at that position.

    I tried to use: QT_NO_CAST_TO_ASCII, in the .pro file, but it seems doesn't work. Anyone can help me? Thank you very much.


  • Lifetime Qt Champion

    @Gabriele11_ said in Assign unsigned char data to QByteArray:

    from "00" to "50" (exadecimal), in output I got "P" at third byte position. I would like to keep the value: "\x50" at that position.

    P is 0x50 so what's your problem? The debug output simply shows you printable characters when possible.



  • Hi,

    my problem is that I should send this byte array to an application that interpret the data as CAN data. So I can't use "P", but I should use the hex value.

    To let you better understand my problem, I should use this QByteArray to fill a can payload like this:

    bufferedFrame.setPayload(canPayload);

    Thanks.


  • Lifetime Qt Champion

    @Gabriele11_ said in Assign unsigned char data to QByteArray:

    To let you better understand my problem, I should use this QByteArray to fill a can payload like this:

    Then do it. All is correct. As I already told you the debugger prints content of a QByteArray in printable chars if possible. Since 0x50 is printable (as 'P') you see a 'P'.



  • Ok,

    I tried to send data directly yo my application, but what I get on app side is very strange.

    I'm sending only one can PGN to the application, with this method:

    "enqueueReceivedFrames(newFrames);"

    This is done every time the PGN is read from the source. On the app side, I'm debugging the frame received flag in this way:

    "qDebug()<<"is valid"<<recFrame.isValid();"

    What I get is a blink between the two values "true" and "false"....seems like the frame is received like true and immediatly like false, and this also reflect the visualization on can data on the app.

    What I set-up before enque the frame is the following:
    ############################################################
    quint32 search_pgn;
    search_pgn = rcv_message.id.is_data_page_1 ? 0x10000:0;
    search_pgn |= ((rcv_message.id.pf<<8)|rcv_message.id.ps);

        if(QString::number(search_pgn,16) == "ff64")
        {
            quint32 frameID = rcv_message.id.sa | search_pgn<<8 | rcv_message.id.priority<<26;
    
            QByteArray canPayload;
            canPayload.setRawData((const char*)rcv_message.data.c,8);
    
            bufferedFrame.setFrameId(frameID);
            bufferedFrame.setPayload(canPayload);
    
            bufferedFrame.setFrameType(QCanBusFrame::DataFrame);
            bufferedFrame.setExtendedFrameFormat(true);
        }
    

    #############################################################

    Am I missing somethig? I read the QT doc on why I can get data frame not valid, but I can't figure out the problem.

    Thank for the help, really appreciated.



  • Ok,

    after going into debug in the QT environment I saw that everything was set correctly.
    The issue related to my problem belongs to the main application, and I finally solved in other ways.

    Thank you all for the support.


Log in to reply