pqendian.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #ifndef PQ_ENDIAN_H
  2. #define PQ_ENDIAN_H
  3. #include <QtEndian>
  4. #if (QT_VERSION >= QT_VERSION_CHECK(5, 7, 0))
  5. #define EndianPointType void
  6. #else
  7. #define EndianPointType uchar
  8. #endif
  9. union FloatUint
  10. {
  11. float _float;
  12. uint _uint;
  13. };
  14. template <>
  15. inline float qFromBigEndian(float src)
  16. {
  17. FloatUint s;
  18. s._float = src;
  19. s._uint = qFromBigEndian(s._uint);
  20. return s._float;
  21. }
  22. template <>
  23. inline float qFromBigEndian<float>(const EndianPointType *src)
  24. {
  25. FloatUint s;
  26. s._uint = qFromBigEndian<uint>(src);
  27. return s._float;
  28. }
  29. template <>
  30. inline float qFromLittleEndian(float src)
  31. {
  32. FloatUint s;
  33. s._float = src;
  34. s._uint = qFromLittleEndian(s._uint);
  35. return s._float;
  36. }
  37. template <>
  38. inline float qFromLittleEndian<float>(const EndianPointType *src)
  39. {
  40. FloatUint s;
  41. s._uint = qFromLittleEndian<uint>(src);
  42. return s._float;
  43. }
  44. template <>
  45. inline float qToBigEndian(float src)
  46. {
  47. FloatUint s;
  48. s._float = src;
  49. s._uint = qToBigEndian(s._uint);
  50. return s._float;
  51. }
  52. template <>
  53. inline void qToBigEndian(float src, EndianPointType *dest)
  54. {
  55. FloatUint s;
  56. s._float = src;
  57. qToBigEndian<uint>(s._uint,dest);
  58. }
  59. template <>
  60. inline float qToLittleEndian(float src)
  61. {
  62. FloatUint s;
  63. s._float = src;
  64. s._uint = qToLittleEndian(s._uint);
  65. return s._float;
  66. }
  67. template <>
  68. inline void qToLittleEndian(float src, EndianPointType *dest)
  69. {
  70. FloatUint s;
  71. s._float = src;
  72. qToLittleEndian<uint>(s._uint,dest);
  73. }
  74. union DoubleUint64
  75. {
  76. double _double;
  77. quint64 _uint64;
  78. };
  79. template <>
  80. inline double qFromBigEndian(double src)
  81. {
  82. DoubleUint64 s;
  83. s._double = src;
  84. s._uint64 = qFromBigEndian(s._uint64);
  85. return s._double;
  86. }
  87. template <>
  88. inline double qFromBigEndian<double>(const EndianPointType *src)
  89. {
  90. DoubleUint64 s;
  91. s._uint64 = qFromBigEndian<quint64>(src);
  92. return s._double;
  93. }
  94. template <>
  95. inline double qFromLittleEndian(double src)
  96. {
  97. DoubleUint64 s;
  98. s._double = src;
  99. s._uint64 = qFromLittleEndian(s._uint64);
  100. return s._double;
  101. }
  102. template <>
  103. inline double qFromLittleEndian<double>(const EndianPointType *src)
  104. {
  105. DoubleUint64 s;
  106. s._uint64 = qFromLittleEndian<quint64>(src);
  107. return s._double;
  108. }
  109. template <>
  110. inline double qToBigEndian(double src)
  111. {
  112. DoubleUint64 s;
  113. s._double = src;
  114. s._uint64 = qToBigEndian(s._uint64);
  115. return s._double;
  116. }
  117. template <>
  118. inline void qToBigEndian(double src, EndianPointType *dest)
  119. {
  120. DoubleUint64 s;
  121. s._double = src;
  122. qToBigEndian(s._uint64,dest);
  123. }
  124. template <>
  125. inline double qToLittleEndian(double src)
  126. {
  127. DoubleUint64 s;
  128. s._double = src;
  129. s._uint64 = qToLittleEndian(s._uint64);
  130. return s._double;
  131. }
  132. template <>
  133. inline void qToLittleEndian(double src, EndianPointType *dest)
  134. {
  135. DoubleUint64 s;
  136. s._double = src;
  137. qToLittleEndian(s._uint64,dest);
  138. }
  139. #endif // ENDIAN_H