How to use Qt documentation?
-
@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) -
@Please_Help_me_D
Apologies.
See https://stackoverflow.com/questions/2945174/floating-point-endianness & https://stackoverflow.com/questions/2782725/converting-float-values-from-big-endian-to-little-endian. I think the latter shows you code to convert. Also https://stackoverflow.com/questions/1786137/c-serialization-of-the-floating-point-numbers-floats-doubles. -
@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 yourfloat
value toquint32
and yourdouble
toquint64
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 forC++ 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 -
@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 yourfloat
value toquint32
and yourdouble
toquint64
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