-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendian.c
71 lines (61 loc) · 1.6 KB
/
endian.c
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
/*
NASA/TRMM, Code 910.1.
This is the TRMM Office Radar Software Library.
Copyright (C) 1996, 1997
John H. Merritt
Space Applications Corporation
Vienna, Virginia
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "endian.h"
int big_endian(void)
{
union {
unsigned char byte[4];
int val;
} word;
word.val = 0;
word.byte[3] = 0x1;
return word.val == 1;
}
int little_endian(void)
{
union {
unsigned char byte[4];
int val;
} word;
word.val = 0;
word.byte[3] = 0x1;
return word.val != 1;
}
void swap_4_bytes(void *word)
{
unsigned char *byte;
unsigned char temp;
byte = word;
temp = byte[0];
byte[0] = byte[3];
byte[3] = temp;
temp = byte[1];
byte[1] = byte[2];
byte[2] = temp;
}
void swap_2_bytes(void *word)
{
unsigned char *byte;
unsigned char temp;
byte = word;
temp = byte[0];
byte[0] = byte[1];
byte[1] = temp;
}