Advertisement
Guest User

Endian Swapper

a guest
Mar 7th, 2013
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. //==============================================================================
  2. //==============================================================================
  3.  
  4. namespace {
  5.  
  6. template <int S> struct swapHelper {
  7.     static void    swap    (unsigned char *v)
  8.     {
  9.         const unsigned int HALF_SIZE = S/2;
  10.         for (unsigned int i = 0; i < HALF_SIZE; ++i) {
  11.        
  12.             unsigned char *a = v+i;
  13.             unsigned char *b = v+S-i-1;
  14.             unsigned char temp = *a;
  15.             *a = *b;
  16.             *b = temp;
  17.         }
  18.     }
  19. };
  20.  
  21. template <> struct swapHelper<1> {
  22.     static void    swap    (unsigned char *v)
  23.     {
  24.         // Do nothing
  25.     }
  26. };
  27.  
  28. template <> struct swapHelper<2> {
  29.     static void    swap    (unsigned char *v)
  30.     {
  31.         unsigned char temp = v[0];
  32.         v[0] = v[1];
  33.         v[1] = temp;
  34.     }
  35. };
  36.  
  37. template <> struct swapHelper<4> {
  38.     static void    swap    (unsigned char *v)
  39.     {
  40.         unsigned char temp = v[0];
  41.         v[0] = v[3];
  42.         v[3] = temp;
  43.  
  44.         temp = v[1];
  45.         v[1] = v[2];
  46.         v[2] = temp;
  47.     }
  48. };
  49.  
  50. template <> struct swapHelper<8> {
  51.     static void    swap    (unsigned char *v)
  52.     {
  53.         unsigned char temp = v[0];
  54.         v[0] = v[7];
  55.         v[7] = temp;
  56.  
  57.         temp = v[1];
  58.         v[1] = v[6];
  59.         v[6] = temp;
  60.  
  61.         temp = v[2];
  62.         v[2] = v[5];
  63.         v[5] = temp;
  64.  
  65.         temp = v[3];
  66.         v[3] = v[4];
  67.         v[4] = temp;
  68.     }
  69. };
  70.  
  71. }
  72.  
  73. //==============================================================================
  74. //==============================================================================
  75.  
  76. class Endian {
  77.     private:
  78.                                 Endian                  (void);
  79.                                 Endian                  (const Endian &rhs);
  80.         Endian &                operator =              (const Endian &rhs);       
  81.                                 ~Endian                 (void);
  82.            
  83.     public:
  84.         /// Swaps endianness of input data
  85.         /// \param v input data to swap
  86.  
  87.         template <typename T>
  88.         static void swap   (T &v)
  89.         {
  90.             swapHelper<sizeof(T)>::swap((unsigned char*)&v);
  91.         }
  92. };
  93.  
  94. //==============================================================================
  95. //==============================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement