Converting chars to QByteArray...



  • ...with the wrinkle that the chars are a hex representation. So I need to take two characters, turn it into a hex value and make that a byte in a QByteArray. I'd do it manually, but these strings are thousands of characters. Any slick way to do this?

    Thanks...


  • Moderators

    @mzimmers It is not clear to me what you are doing. A char as at least one byte - how do you put two chars into one byte?
    To put a char into QByteArray:

    char ch = 0x42;
    QByteArray array;
    array.append(ch);
    

    Here http://doc.qt.io/qt-5/qbytearray.html you can find this example:

    const char cart[] = {'c', 'a', '\0', 'r', '\0', 't'};
    QByteArray ba4(QByteArray::fromRawData(cart, 6));
    ba4.size();                     // Returns 6.
    ba4.constData();                // Returns "ca\0r\0t" without terminating \0.
    


  • @mzimmers: QByteArray::fromHex()

    Edit: direct link is: http://doc.qt.io/qt-5/qbytearray.html#fromHex



  • Here's what I'm talking about: I'm given a string like this:

        char  Txt8[] =                                              //  Len = 1054 BYTES
                    "e5098b6a0b1cfc57c6a76537104a39c48baecb15c6bbb46fbb0b745f9c9e5c05"
                    "cfcfabb33786f7b7b5b0ce74eeec9eb84f87d2494fab3ec1f4d3bd9c99821890"
                    "ee352a1d40964264fbf2c93c6ded2583cc75dcb27bf4fdb489cabcf97bfa5cc6"
                    "4b2352cfb0b3a707a0579eb713b697cd0b5e3377d1feb9f181d7b89cc86dee4f"
                    "ed8269f10e44ec48adc6940c6badbb40122c1dc2d9323920e4e1fbad0b4397d4"
                    "dc38b8ade3b3dace2926f464fa3b5b82ebc5e3b81cf647e8bbd2cb55c9e31ffd"
                    "212f8729b66739421c6106e64ac83d3b9e13cd8321b3a9f10d9171bb8cb74e71"
                    "c34d1e8d0fc8d14b8e5e12bbe2bd2a1431fc224b70d228e4e2063509db26ecd9"
                    "ca7cc402763e69928805600a4a80eab4ae6a2c3792b98c6942195e643f98c0dc"
                    "3fa3c2b07431cbbe113e38fc0b7b45c51c4431700ed29d2736b236f63f759323"
                    "29aa60be9009bd7832f1e1b9ac1503ec84727a1e6c8423c7c5b903e763262d55"
                    "9078e654532e0868f206a468b5b5ebd3eddb4f673536e5f0f8160e5f3311561b"
                    "7cf79c9c440974355965c931aec5c7225f69f776f052ac4bd6b19f85389fd61d"
                    "f60ecabbeb00c8886ff7983d20ac5d81e303bc71253f40806772fd81f9387402"
                    "05a5b7dcd07cce083da258b493d275967f91e4815d656936b342727cfe45f973"
                    "b2a5ac257ce64c5eca4f53be8d9fd90c3dfcb8cd1e2cef15c307449ed02c2e17"
                    "04f4f1be76a40b311ee7cf81987b5089252a807ef3fc99c79eabbc0ef657d897"
                    "037bced04620d32a425015283bcea1b53e0484bb613d30f14c1422f5f82cc29a"
                    "b7228b8375c06bf13d746dd9ff00953a90720badf2577d3ed62cbe7a5f15b3c9"
                    "29d26ffe8aee9d2d17391ebc6a79f4bd235d5f7b2db2455343d9d7c6b27972cc"
                    "6071c36a0d112f86d98972fb06a186e900abc64e9ab653db9b05b70079c0c84a"
                    "64e8cfee8690eaa68a4bafbb5be112632e46894ec2cc6e7ce697a4513d517deb"
                    "3e20dbb37ed5963232671e27ef9f62d6b514f0a22c5a5dde2d77e7e184965958"
                    "f5002fe17d47fbd5d9c407644d443ce89eff427360cae9aa788dc8d7d9f62439"
                    "916f139f094ee035884cb29dfa396941f0eec9e8e782da88cdc18e5bc1d9a535"
                    "1b57ce15ac520ffa47e666f87fe5b18ab3c8cb2a48ecf81f36fb8397c6a7a5f5"
                    "9a9fa96cedbb4ecd1c7a6d9d65afdb6bef7791600b6e0a18ba23edb06fc9ec21"
                    "162feccc54f2665611f10db53401b18bade263b3b972da1a612115d144a54260"
                    "97efdf5c6a5d1f3c2d318f687242f993f5f1884bd95f2ece34dd4320cea46f5a"
                    "26c7c945b665402778233bdda9d97c2acd8c4a4ff39dcfdc3a3fbfc5942e3ab8"
                    "ca9ff4aec17293c1fbaf583d603002f93f9befe8909485eb7c30d0e91fac6c22"
                    "8c5fa6c011eddeafbdbe30af20ae53a85206c03d37ac17a30096bfb4f584cd3f"
                    "72ef28a3303cea9cc636095c70bb36de0eb50577704d4faed05bd54da020";
    

    I need to format 2 characters at a time into hex, and store them in a QByteArray. So, the first byte would take the first two characters ("e5") and create a byte of value 0xe5.

    Edit: this works, but is ugly (IMO):

        QByteArray qTxt;
        uint8_t c;
        char *p;
    
        for (int i = 0; i < strlen(Txt8); i += 2)
        {
            p = Txt8 + i;
            sscanf(p, "%2x", &c);
            qTxt.append((char) c);
        }
    

    I was just curious whether anyone had knowledge of a way to reduce this mess to a single line...



  • QByteArray qTxt("\all your hex here", 1054);
    
    // for loop to grab the first byte in the array
    

    The code you posted should work fine. While do might work as well.



  • Aha's solution is the winner. Thanks, everyone.


Log in to reply
 

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