-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbyteconverter.c
163 lines (138 loc) · 4.03 KB
/
byteconverter.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <stdio.h>
#include "byteconverter.h"
short byte2short(const byte* src, const unsigned short offset) {
byte buf[2];
buf[0] = src[offset ];
buf[1] = src[offset + 1];
return *((short *)buf);
}
unsigned short byte2ushort(const byte* src, const unsigned short offset) {
byte buf[2];
buf[0] = src[offset ];
buf[1] = src[offset + 1];
return *((unsigned short *)buf);
}
int byte2int(const byte* src, const unsigned short offset) {
byte buf[4];
buf[0] = src[offset ];
buf[1] = src[offset + 1];
buf[2] = src[offset + 2];
buf[3] = src[offset + 3];
return *((int *)buf);
}
unsigned int byte2uint(const byte* src, const unsigned short offset) {
byte buf[4];
buf[0] = src[offset ];
buf[1] = src[offset + 1];
buf[2] = src[offset + 2];
buf[3] = src[offset + 3];
return *((unsigned int *)buf);
}
long byte2long(const byte* src, const unsigned short offset) {
byte buf[8];
buf[0] = src[offset ];
buf[1] = src[offset + 1];
buf[2] = src[offset + 2];
buf[3] = src[offset + 3];
buf[4] = src[offset + 4];
buf[5] = src[offset + 5];
buf[6] = src[offset + 6];
buf[7] = src[offset + 7];
return *((long *)buf);
}
unsigned long byte2ulong(const byte* src, const unsigned short offset) {
byte buf[8];
buf[0] = src[offset ];
buf[1] = src[offset + 1];
buf[2] = src[offset + 2];
buf[3] = src[offset + 3];
buf[4] = src[offset + 4];
buf[5] = src[offset + 5];
buf[6] = src[offset + 6];
buf[7] = src[offset + 7];
return *((unsigned long *)buf);
}
float byte2float(const byte* src, const unsigned short offset) {
byte buf[4];
buf[0] = src[offset ];
buf[1] = src[offset + 1];
buf[2] = src[offset + 2];
buf[3] = src[offset + 3];
return *((float *)buf);
}
double byte2double(const byte* src, const unsigned short offset) {
byte buf[8];
buf[0] = src[offset ];
buf[1] = src[offset + 1];
buf[2] = src[offset + 2];
buf[3] = src[offset + 3];
buf[4] = src[offset + 4];
buf[5] = src[offset + 5];
buf[6] = src[offset + 6];
buf[7] = src[offset + 7];
return *((double *)buf);
}
void short2byte(byte* dst, const unsigned short offset, const short src) {
byte *bufp = (byte *)&src;
dst[offset ] = bufp[0];
dst[offset + 1] = bufp[1];
}
void ushort2byte(byte* dst, const unsigned short offset, const unsigned short src) {
byte *bufp = (byte *)&src;
dst[offset ] = bufp[0];
dst[offset + 1] = bufp[1];
}
void int2byte(byte* dst, const unsigned short offset, const int src) {
byte *bufp = (byte *)&src;
dst[offset ] = bufp[0];
dst[offset + 1] = bufp[1];
dst[offset + 2] = bufp[2];
dst[offset + 3] = bufp[3];
}
void uint2byte(byte* dst, const unsigned short offset, const unsigned int src) {
byte *bufp = (byte *)&src;
dst[offset ] = bufp[0];
dst[offset + 1] = bufp[1];
dst[offset + 2] = bufp[2];
dst[offset + 3] = bufp[3];
}
void long2byte(byte* dst, const unsigned short offset, const long src) {
byte *bufp = (byte *)&src;
dst[offset ] = bufp[0];
dst[offset + 1] = bufp[1];
dst[offset + 2] = bufp[2];
dst[offset + 3] = bufp[3];
dst[offset + 4] = bufp[4];
dst[offset + 5] = bufp[5];
dst[offset + 6] = bufp[6];
dst[offset + 7] = bufp[7];
}
void ulong2byte(byte* dst, const unsigned short offset, const unsigned long src) {
byte *bufp = (byte *)&src;
dst[offset ] = bufp[0];
dst[offset + 1] = bufp[1];
dst[offset + 2] = bufp[2];
dst[offset + 3] = bufp[3];
dst[offset + 4] = bufp[4];
dst[offset + 5] = bufp[5];
dst[offset + 6] = bufp[6];
dst[offset + 7] = bufp[7];
}
void float2byte(byte* dst, const unsigned short offset, const float src) {
byte *bufp = (byte *)&src;
dst[offset ] = bufp[0];
dst[offset + 1] = bufp[1];
dst[offset + 2] = bufp[2];
dst[offset + 3] = bufp[3];
}
void double2byte(byte* dst, const unsigned short offset, const double src) {
byte *bufp = (byte *)&src;
dst[offset ] = bufp[0];
dst[offset + 1] = bufp[1];
dst[offset + 2] = bufp[2];
dst[offset + 3] = bufp[3];
dst[offset + 4] = bufp[4];
dst[offset + 5] = bufp[5];
dst[offset + 6] = bufp[6];
dst[offset + 7] = bufp[7];
}