File: ByteSwapping.cxx

package info (click to toggle)
caret 5.6.4~dfsg.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 31,904 kB
  • ctags: 28,901
  • sloc: cpp: 378,050; python: 6,718; ansic: 5,507; makefile: 333; sh: 46
file content (106 lines) | stat: -rw-r--r-- 1,830 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

#include "ByteSwapping.h"

/**
 * Swap bytes for the specified type.
 */
void 
ByteSwapping::swapBytes(short* n, int numToSwap)
{
   for (int i = 0; i < numToSwap; i++) {
      char* bytes = (char*)&n[i];
      char  temp = bytes[0];
      bytes[0] = bytes[1];
      bytes[1] = temp;
   }
}

/**
 * Swap bytes for the specified type.
 */
void 
ByteSwapping::swapBytes(unsigned short* n, int numToSwap)
{
   swapBytes((short*)n, numToSwap);
}

/**
 * Swap bytes for the specified type.
 */
void 
ByteSwapping::swapBytes(int* n, int numToSwap)
{
   for (int i = 0; i < numToSwap; i++) {
      char* bytes = (char*)&n[i];
      char  temp = bytes[0];
      bytes[0] = bytes[3];
      bytes[3] = temp;

      temp = bytes[1];
      bytes[1] = bytes[2];
      bytes[2] = temp;
   }
}

/**
 *
 */
void 
ByteSwapping::swapBytes(unsigned int* n, int numToSwap)
{
   swapBytes((int*)n, numToSwap);
}

/**
 * Swap bytes for the specified type.
 */
void 
ByteSwapping::swapBytes(long long* n, int numToSwap)
{
   for (int i = 0; i < numToSwap; i++) {
      char* bytes = (char*)&n[i];
      char  temp = bytes[0];
      bytes[0] = bytes[7];
      bytes[7] = temp;

      temp = bytes[1];
      bytes[1] = bytes[6];
      bytes[6] = temp;

      temp = bytes[2];
      bytes[2] = bytes[5];
      bytes[5] = temp;

      temp = bytes[3];
      bytes[3] = bytes[4];
      bytes[4] = temp;
   }
}

/**
 * Swap bytes for the specified type.
 */
void 
ByteSwapping::swapBytes(unsigned long long* n, int numToSwap)
{
   swapBytes((long long*)n, numToSwap);
}

/**
 * Swap bytes for the specified type.
 */
void 
ByteSwapping::swapBytes(float* n, int numToSwap)
{
   swapBytes((int*)n, numToSwap);
}

/**
 * Swap bytes for the specified type.
 */
void 
ByteSwapping::swapBytes(double* n, int numToSwap)
{
   swapBytes((long long*)n, numToSwap);
}