TYCHO  1.3.0
 All Data Structures Files Functions Variables Enumerations Enumerator
/home/kapf/tycho_docu/big_endian_converter.c
Go to the documentation of this file.
1 /*
2  * big_endian_converter.c
3  *
4  * Author: Wolfgang Kapferer
5  * Thanks to
6  * http://www.linuxquestions.org/questions/programming-9/c-function-to-reverse-the-byte-order-in-a-double-825743/
7  */
8 
9 #include <stdio.h>
10 #include <stdint.h>
11 #include <stdlib.h>
12 
13 #include "variables_global.h"
14 #include "prototypes.h"
15 
21 long long int ntohll(const long long int data) {
22 
23  enum {
24  TYP_INIT, TYP_SMLE, TYP_BIGE
25  };
26 
27  union {
28  long long int ull;
29  uint8_t c[8];
30  } x;
31 
32  // Test if on BigEndian machine
33  static int typ = TYP_INIT;
34 
35  if (typ == TYP_INIT) {
36  x.ull = 0x01;
37  typ = (x.c[7] == 0x01) ? TYP_BIGE : TYP_SMLE;
38  }
39 
40  // If system is BigEndian; return data as is.
41  if (typ == TYP_BIGE) {
42  return data;
43  }
44 
45  // convert data to Big Endian
46  x.ull = data;
47 
48  int8_t c = 0;
49  c = x.c[0];
50  x.c[0] = x.c[7];
51  x.c[7] = c;
52  c = x.c[1];
53  x.c[1] = x.c[6];
54  x.c[6] = c;
55  c = x.c[2];
56  x.c[2] = x.c[5];
57  x.c[5] = c;
58  c = x.c[3];
59  x.c[3] = x.c[4];
60  x.c[4] = c;
61 
62  return x.ull;
63 }