endian.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #if !defined(ENDIAN_HPP)
  2. #define ENDIAN_HPP
  3. //______________________________________________________________________________
  4. //
  5. // endian.hpp
  6. // Facilitate endian conversions.
  7. //namespace xxx
  8. //{
  9. //--------------------------------------
  10. inline
  11. bool big_endian()
  12. {
  13. long lTest = 1;
  14. return (reinterpret_cast<char*>(&lTest)[0] == 0);
  15. }
  16. //--------------------------------------
  17. inline
  18. short swap_endian(short i16)
  19. {
  20. return i16 << 8 & 0xff00
  21. | i16 >> 8 & 0x00ff;
  22. }
  23. //--------------------------------------
  24. inline
  25. unsigned short swap_endian(unsigned short ui16)
  26. {
  27. return ui16 << 8 & 0xff00
  28. | ui16 >> 8 & 0x00ff;
  29. }
  30. //--------------------------------------
  31. inline
  32. unsigned int swap_endian(unsigned int i32)
  33. {
  34. return i32 << 24 & 0xff000000
  35. | i32 << 8 & 0x00ff0000
  36. | i32 >> 8 & 0x0000ff00
  37. | i32 >> 24 & 0x000000ff;
  38. }
  39. //--------------------------------------
  40. inline
  41. int swap_endian(int ui32)
  42. {
  43. return ui32 << 24 & 0xff000000
  44. | ui32 << 8 & 0x00ff0000
  45. | ui32 >> 8 & 0x0000ff00
  46. | ui32 >> 24 & 0x000000ff;
  47. }
  48. //--------------------------------------
  49. inline
  50. short big_endian(short i16Native)
  51. {
  52. if (big_endian())
  53. {
  54. return i16Native;
  55. }
  56. return swap_endian(i16Native);
  57. }
  58. //--------------------------------------
  59. inline
  60. unsigned short big_endian(unsigned short ui16Native)
  61. {
  62. if (big_endian())
  63. {
  64. return ui16Native;
  65. }
  66. return swap_endian(ui16Native);
  67. }
  68. //--------------------------------------
  69. inline
  70. unsigned int big_endian(unsigned int i32Native)
  71. {
  72. if (big_endian())
  73. {
  74. return i32Native;
  75. }
  76. return swap_endian(i32Native);
  77. }
  78. //--------------------------------------
  79. inline
  80. int big_endian(int ui32Native)
  81. {
  82. if (big_endian())
  83. {
  84. return ui32Native;
  85. }
  86. return swap_endian(ui32Native);
  87. }
  88. //--------------------------------------
  89. inline
  90. short little_endian(short i16Native)
  91. {
  92. if (!big_endian())
  93. {
  94. return i16Native;
  95. }
  96. return swap_endian(i16Native);
  97. }
  98. //--------------------------------------
  99. inline
  100. unsigned short little_endian(unsigned short ui16Native)
  101. {
  102. if (!big_endian())
  103. {
  104. return ui16Native;
  105. }
  106. return swap_endian(ui16Native);
  107. }
  108. //--------------------------------------
  109. inline
  110. unsigned int little_endian(unsigned int i32Native)
  111. {
  112. if (!big_endian())
  113. {
  114. return i32Native;
  115. }
  116. return swap_endian(i32Native);
  117. }
  118. //--------------------------------------
  119. inline
  120. int little_endian(int ui32Native)
  121. {
  122. if (!big_endian())
  123. {
  124. return ui32Native;
  125. }
  126. return swap_endian(ui32Native);
  127. }
  128. //--------------------------------------
  129. inline
  130. short native_endian(short i16, bool bBigEndian)
  131. {
  132. if (big_endian() != bBigEndian)
  133. {
  134. return swap_endian(i16);
  135. }
  136. return i16;
  137. }
  138. //--------------------------------------
  139. inline
  140. unsigned short native_endian(unsigned short ui16, bool bBigEndian)
  141. {
  142. if (big_endian() != bBigEndian)
  143. {
  144. return swap_endian(ui16);
  145. }
  146. return ui16;
  147. }
  148. //--------------------------------------
  149. inline
  150. unsigned int native_endian(unsigned int i32, bool bBigEndian)
  151. {
  152. if (big_endian() != bBigEndian)
  153. {
  154. return swap_endian(i32);
  155. }
  156. return i32;
  157. }
  158. //--------------------------------------
  159. inline
  160. int native_endian(int ui32, bool bBigEndian)
  161. {
  162. if (big_endian() != bBigEndian)
  163. {
  164. return swap_endian(ui32);
  165. }
  166. return ui32;
  167. }
  168. //} // namespace xxx
  169. #endif // ENDIAN_HPP