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

How to use Qt documentation?



  • Good evening,
    I can see that Qt has good documentation but not enough examples. Usually I understand how to use the fucntion but now I'm stuck...
    I'm trying to use the following function: Qt_ToBigEndian
    I can see the syntaxis:

    void qToBigEndian(const void *src, qsizetype count, void *dest)
    

    where (as I understood):
    *src - pointer to the my data that I need to convert to big endian
    count - number of bytes (or elements) to convert to big endian
    *dest - destination pointer (pointer to memory allocation where I want to get big-ordered data)

    So I write:

        int a[] = {1, 2, 3, 4, 5};
        int x[5];
        qToBigEndian(a, 5, x);
    

    and of course I get errors :)
    What is wrong?
    !______________________________________________________!
    main.cpp:12:5: error: no matching function for call to 'qToBigEndian'
    qendian.h:238:35: note: candidate template ignored: couldn't infer template argument 'T'
    qendian.h:225:49: note: candidate function template not viable: requires single argument 'source', but 3 arguments were provided
    qendian.h:233:35: note: candidate function template not viable: requires 2 arguments, but 3 were provided

    By the way the stuff:

        for (int i = 0; i < 5; i++)
        {
            cout << qToBigEndian(a[i]) << endl;
        }
    

    goes ok


  • Lifetime Qt Champion

    Hi
    That one is a bit tricky, i agree.
    It does not really scream "Hey its a template function."

    The docs says
    "
    Note: Template type T can either be a quint16, qint16, quint32, qint32, quint64, or qint64. Other types of integers, e.g., qlong, are not applicable."

    so something like

    quint32 a[] = {1, 2, 3, 4, 5};
    quint32 x[5];
    qToBigEndian<quint32>(a, 5,x);
    

  • Lifetime Qt Champion

    Hi
    That one is a bit tricky, i agree.
    It does not really scream "Hey its a template function."

    The docs says
    "
    Note: Template type T can either be a quint16, qint16, quint32, qint32, quint64, or qint64. Other types of integers, e.g., qlong, are not applicable."

    so something like

    quint32 a[] = {1, 2, 3, 4, 5};
    quint32 x[5];
    qToBigEndian<quint32>(a, 5,x);
    


  • @mrjj thank you!
    In reality I also need to use Little/Big conversation with float data-type. Do you know how to solve that?



  • @Please_Help_me_D
    Shoot me if I'm wrong, but I don't think float/doubles have any "endian". They are stored according to some IEEE standard.



  • @JonB I don't have a gun :D
    but as far as I know (and according to my experience in Matlab) files can be stored in any type (int, float, double etc) and in either big- or little-endian
    Also my data maybe stored in IEEE format but there is also IBM format (it is old format but still actively used while storing SEGY files)





  • @JonB Thank you!
    First of all I'm trying to use built-in function. And I liked Qt built in function because it can deal with array, that is what I like :)
    What <quint32> mean in this example?

    quint32 a[] = {1, 2, 3, 4, 5};
    quint32 x[5];
    qToBigEndian<quint32>(a, 5,x);
    


  • @Please_Help_me_D said in How to use Qt documentation?:

    In reality I also need to use Little/Big conversation with float data-type. Do you know how to solve that?

    As far as I know, a float is stored as a 32 bits word and a double as 64 bits word.
    So my suggestion would be to cast your float value to quint32 and your double to quint64

    float floatBE = static_cast<float>(qToBigEndian(static_cast<quint32>(val));
    double doubleBE = static_cast<double>(qToBigEndian(static_cast<quint64>(valDouble));
    


  • @Please_Help_me_D
    quint32 is (presumably) Qt's type for an unsigned 32-bit integer.
    qToBigEndian<quint32> is written like that presumably because it's a template class/function.



  • @KroMignon thank you! that should work, few minuts I'm going to try it
    @JonB please correct me if I misunderstood you, we need to write datatype in brackets <> to point that incoming (or outcoming??) parameter is in quint32 because qToBigEndian is a template function?
    I just did:

        double y[] = {1.442, 2.34, 1.56, 2.66, 68.88};
        double yy[5];
        qToLittleEndian<double>(y, 5, yy);
    

    and it doesn't output the error but I need to check the correctness of this



  • @Please_Help_me_D
    It's not a "parameter" in the sense of passing to a function. You really need to go Google for C++ template if you wish to understand C++ templates.



  • @JonB ok I look for that
    I tryed to convert double precision point in Qt and in Matlab to check the correctness of little/big endian conversation. So in Qt:

        double y[] = {1.442, 2.34, 1.56, 2.66, 68.88};
        double yy[5];
        qToLittleEndian<double>(y, 5, yy);
    

    yy = {-1.291069932956366e+151, -2.242484837845019e-38, -1.498274909768364e+261, 1.217960639251452e+43, -2.242484835660769e-38};

    In Matlab:

    // Matlab code
    y = [1.442, 2.34, 1.56, 2.66, 68.88];
    yy = swapbytes(y); // should rearrange the byte order [swapbyte_documentation](https://www.mathworks.com/help/matlab/ref/swapbytes.html)
    

    yy = [-1.29106993295637e+151, -2.24248483784502e-38, -1.49827490976836e+261, 1.21796063925145e+43, -2.24248483566077e-38];

    The Matlab result is very close to Qt result. So I think that Qt function qToLittleEndian works not only with qType but with other standart types as well



  • @Please_Help_me_D said in How to use Qt documentation?:

    I need to check the correctness of this

    Look up how a given value is stored in big endian and little endian. Then print out the bytes of your variable as hex. Do this before and after conversion. You should be able to see that it rearranges the bytes in an expected way. It might be worthwhile to create your own function to do this as well. So you understand more what is going on. It might also be worthwhile to templatize your function so you can see how templates work too.



  • @fcarney thank you. I already did it with the help of Matlab above. I just converted the same numbers of double precision from Big to Little and the result was the same.
    I understand how it works, it is clear from the definition of Little/Big endian and definition of byte


  • Lifetime Qt Champion

    @KroMignon said in How to use Qt documentation?:

    @Please_Help_me_D said in How to use Qt documentation?:

    In reality I also need to use Little/Big conversation with float data-type. Do you know how to solve that?

    As far as I know, a float is stored as a 32 bits word and a double as 64 bits word.
    So my suggestion would be to cast your float value to quint32 and your double to quint64

    float floatBE = static_cast<float>(qToBigEndian(static_cast<quint32>(val));
    double doubleBE = static_cast<double>(qToBigEndian(static_cast<quint64>(valDouble));
    

    nope, that does NOT work! Casting float to int truncates it. What you can do instead, is to reinterpret_cast the memory occupied by the float - or use a union. We currently have a similar topic in the forum on this.

    Regards



  • I have written the following code:

        if (fileEndian == "Little"){
            for(quint32 i = 0; i < nTrc; i++){
                FFID(i) = *util::bit_cast<qint32*>(qFromLittleEndian(qFile->map(3608+i*bytesPerTrc, 1)));
            }
        } else if (fileEndian == "Big"){
            for(quint32 i = 0; i < nTrc; i++){
                FFID(i) = *util::bit_cast<qint32*>(qFromBigEndian(qFile->map(3608+i*bytesPerTrc, 1)));
            }
        }
    

    It gives me errors:

    • readsegy.obj:-1: ошибка: LNK2019: unresolved external symbol "unsigned char * __cdecl qbswap<unsigned char *>(unsigned char *)" (??$qbswap@PEAE@@YAPEAEPEAE@Z) referenced in function "unsigned char * __cdecl qFromBigEndian<unsigned char *>(unsigned char *)" (??$qFromBigEndian@PEAE@@YAPEAEPEAE@Z)
    • debug\ReadSegy.exe:-1: ошибка: LNK1120: 1 unresolved externals

    The compilator output:
    jom: C: \ Users \ tasik \ Documents \ Qt_Projects \ build-ReadSegy-Desktop_x86_windows_msvc2017_pe_64bit-Debug \ Makefile.Debug [debug \ ReadSegy.exe] Error 1120
    jom: C: \ Users \ tasik \ Documents \ Qt_Projects \ build-ReadSegy-Desktop_x86_windows_msvc2017_pe_64bit-Debug \ Makefile [debug] Error 2
    19:05:31: The process "C: \ Qt \ Tools \ QtCreator \ bin \ jom.exe" ended with code 2.
    Error during assembly / deployment of ReadSegy project (bundle: Desktop (x86-windows-msvc2017-pe-64bit))
    During the execution of the "Assembly"

    Actually the problem was that I didn't know the line which throws these errors but by intuation I just commented the BigEndian part of the code and it works:

        if (fileEndian == "Little"){
            for(quint32 i = 0; i < nTrc; i++){
                FFID(i) = *util::bit_cast<qint32*>(qFromLittleEndian(qFile->map(3608+i*bytesPerTrc, 1)));
            }
        } else if (fileEndian == "Big"){/*
            for(quint32 i = 0; i < nTrc; i++){
                FFID(i) = *util::bit_cast<qint32*>(qFromBigEndian(qFile->map(3608+i*bytesPerTrc, 1)));
            }*/
        }
    

    I use little endian Windows 10 x64, Qt 5.14.0, MSVC 2017 x64.
    Why do I can use qFromLittleEndian but I can't qFromBigEndian??

    By the way the endian of my file is LITTLE now

    I think I just found a solution. If I change the order of performing bit_cast and qFromBigEndian it works:

        if (fileEndian == "Little"){
            for(quint32 i = 0; i < nTrc; i++){
                FFID(i) = qFromLittleEndian(*util::bit_cast<qint32*>(qFile->map(3608+i*bytesPerTrc, 1)));
            }
        } else if (fileEndian == "Big"){
            for(quint32 i = 0; i < nTrc; i++){
                FFID(i) = qFromBigEndian(*util::bit_cast<qint32*>(qFile->map(3608+i*bytesPerTrc, 1)));
            }
        }
    

    I don't understand why but that works fine


Log in to reply