-
Notifications
You must be signed in to change notification settings - Fork 9
/
BigEndian.cs
177 lines (156 loc) · 5.45 KB
/
BigEndian.cs
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using System.IO;
using System.Text;
namespace NSpeex
{
/// <summary>
/// BigEndian
/// </summary>
public class BigEndian
{
private static byte[] Convert(byte[] data)
{
if (BitConverter.IsLittleEndian)
{
// revert order
int size = data.Length;
byte[] result = new byte[size];
for (int i = 0; i < data.Length; i++)
{
result[i] = data[size-1-i];
}
return result;
}
else
{
return data;
}
}
public static void WriteShort(byte[] buf,int offset,short value)
{
// a short has 2 bytes
byte[] data = BitConverter.GetBytes(value);
byte[] data2 = Convert(data);
Array.Copy(data2, 0, buf, offset, 2);
}
public static void WriteShort(Stream stream,short value)
{
// a short has 2 bytes
byte[] data = BitConverter.GetBytes(value);
byte[] data2 = Convert(data);
stream.Write(data2,0,data2.Length);
}
/// <summary>
/// write a int value to buf where index is offset,
/// that means from buf[offset] to buf[offset + 3] is value data
/// </summary>
public static void WriteInt(byte[] buf, int offset, int value)
{
byte[] data = BitConverter.GetBytes(value);
byte[] data2 = Convert(data);
Array.Copy(data2, 0, buf, offset, 4);
}
public static void WriteInt(Stream stream, int value)
{
byte[] data = BitConverter.GetBytes(value);
byte[] data2 = Convert(data);
stream.Write(data2,0,data2.Length);
}
/// <summary>
/// write a long value to buf where index is offset,
/// that means from buf[offset] to buf[offset + 7] is value data
/// </summary>
public static void WriteLong(byte[] buf, int offset, long value)
{
byte[] data = BitConverter.GetBytes(value);
byte[] data2 = Convert(data);
Array.Copy(data2, 0, buf, offset, 8);
}
public static void WriteLong(Stream stream, long value)
{
byte[] data = BitConverter.GetBytes(value);
byte[] data2 = Convert(data);
stream.Write(data2,0,data2.Length);
}
/// <summary>
/// write a string value to buf where index is offset,
/// that means from buf[offset] to buf[offset + value.Length] is value data
/// </summary>
public static void WriteString(byte[] buf, int offset, string value)
{
byte[] data = UTF8Encoding.UTF8.GetBytes(value);
Array.Copy(data, 0, buf, offset, data.Length);
}
public static void WriteString(Stream stram,string value)
{
byte[] data = UTF8Encoding.UTF8.GetBytes(value);
stram.Write(data,0,data.Length);
}
public static short ReadShort(byte[] buf, int offset)
{
byte[] data = new byte[2] { 0, 0 };
Array.Copy(buf, offset, data, 0, 2);
if (BitConverter.IsLittleEndian)
{
byte tmp = data[0];
data[0] = data[1];
data[1] = tmp;
}
return BitConverter.ToInt16(data, 0);
}
public static short ReadShort(Stream stream)
{
byte[] data = new byte[2] { 0, 0 };
stream.Read(data, 0, 2);
byte[] data2 = Convert(data);
return BitConverter.ToInt16(data2, 0);
}
/// <summary>
/// read value from buf where index is offset
/// </summary>
public static int ReadInt(byte[] buf, int offset)
{
byte[] data = new byte[4] { 0, 0, 0, 0 };
Array.Copy(buf, offset, data, 0, 4);
byte[] data2 = Convert(data);
return BitConverter.ToInt32(data2, 0);
}
public static int ReadInt(Stream stream)
{
byte[] data = new byte[4] { 0, 0, 0, 0 };
stream.Read(data, 0, 4);
byte[] data2 = Convert(data);
return BitConverter.ToInt32(data2, 0);
}
/// <summary>
/// read value from buf where index is offset
/// </summary>
public static long ReadLong(byte[] buf, int offset)
{
byte[] data = new byte[8] { 0, 0, 0, 0, 0, 0, 0, 0 };
Array.Copy(buf, offset, data, 0, 8);
byte[] data2 = Convert(data);
return BitConverter.ToInt64(data2, 0);
}
public static long ReadLong(Stream stream)
{
byte[] data = new byte[8] { 0, 0, 0, 0, 0, 0, 0, 0 };
stream.Read(data, 0, 8);
byte[] data2 = Convert(data);
return BitConverter.ToInt64(data2, 0);
}
/// <summary>
/// read value from buf where index is offset and length is size
/// </summary>
public static string ReadString(byte[] buf, int offset, int size)
{
return UTF8Encoding.UTF8.GetString(buf, offset, size);
}
public static string ReadString(Stream stream,int size)
{
byte[] data = new byte[size];
stream.Read(data, 0, size);
return UTF8Encoding.UTF8.GetString(data, 0, size);
}
}
}