Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BitConverter.cs « System « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e48ab5df46ada3d07948a4f88fe120cd0616f9c0 (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//
// System.BitConverter.cs
//
// Author:
//   Matt Kimball (matt@kimball.net)
//

//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

namespace System
{
	public sealed class BitConverter
	{
		public static readonly bool IsLittleEndian = AmILittleEndian ();

		private BitConverter ()
		{
		}

		static bool AmILittleEndian ()
		{
			byte[] one = GetBytes ((int) 1);
			return (one [0] == 1);
		}

		public static long DoubleToInt64Bits (double value)
		{
			return ToInt64 (GetBytes (value), 0);
		}

		public static double Int64BitsToDouble (long value)
		{
			return ToDouble (GetBytes (value), 0);
		}

		unsafe static byte[] GetBytes (byte *ptr, int count)
		{
			byte [] ret = new byte [count];

			for (int i = 0; i < count; i++) {
				ret [i] = ptr [i];
			}

			return ret;
		}

		unsafe public static byte[] GetBytes (bool value)
		{
			return GetBytes ((byte *) &value, 1);
		}

		unsafe public static byte[] GetBytes (char value)
		{
			return GetBytes ((byte *) &value, 2);
		}

		unsafe public static byte[] GetBytes (short value)
		{
			return GetBytes ((byte *) &value, 2);
		}

		unsafe public static byte[] GetBytes (int value)
		{
			return GetBytes ((byte *) &value, 4);
		}

		unsafe public static byte[] GetBytes (long value)
		{
			return GetBytes ((byte *) &value, 8);
		}

		[CLSCompliant (false)]
		unsafe public static byte[] GetBytes (ushort value)
		{
			return GetBytes ((byte *) &value, 2);
		}

		[CLSCompliant (false)]
		unsafe public static byte[] GetBytes (uint value)
		{
			return GetBytes ((byte *) &value, 4);
		}

		[CLSCompliant (false)]
		unsafe public static byte[] GetBytes (ulong value)
		{
			return GetBytes ((byte *) &value, 8);
		}

		unsafe public static byte[] GetBytes (float value)
		{
			return GetBytes ((byte *) &value, 4);
		}

		unsafe public static byte[] GetBytes (double value)
		{
			return GetBytes ((byte *) &value, 8);
		}

		unsafe static void PutBytes (byte *dst, byte[] src, int start_index, int count)
		{
			if (src == null) {
				throw new ArgumentNullException ("value"); // gets called from methods with value params
			}

			if (start_index < 0)
				throw new ArgumentOutOfRangeException ("startIndex < 0");

			// avoid integer overflow (with large pos/neg start_index values)
			if (src.Length - count < start_index) {
				throw new ArgumentOutOfRangeException (Locale.GetText (
					"Value is too big to return the requested type."), "startIndex");
			}

			for (int i = 0; i < count; i++) {
				dst[i] = src[i + start_index];
			}
		}

		unsafe public static bool ToBoolean (byte[] value, int startIndex)
		{
			if (value == null) 
				throw new ArgumentNullException ("value");

			if (startIndex < 0)
				throw new ArgumentOutOfRangeException ("startIndex < 0");

			// avoid integer overflow (with large pos/neg start_index values)
			if (value.Length - 1 < startIndex) 
				throw new ArgumentOutOfRangeException (Locale.GetText (
					"Value is too big to return the requested type."), "startIndex");

			if (value [startIndex] != 0)
				return true;
			
			return false;
		}

		unsafe public static char ToChar (byte[] value, int startIndex)
		{
			char ret;

			PutBytes ((byte *) &ret, value, startIndex, 2);

			return ret;
		}

		unsafe public static short ToInt16 (byte[] value, int startIndex)
		{
			short ret;

			PutBytes ((byte *) &ret, value, startIndex, 2);

			return ret;
		}

		unsafe public static int ToInt32 (byte[] value, int startIndex)
		{
			int ret;

			PutBytes ((byte *) &ret, value, startIndex, 4);

			return ret;
		}

		unsafe public static long ToInt64 (byte[] value, int startIndex)
		{
			long ret;

			PutBytes ((byte *) &ret, value, startIndex, 8);

			return ret;
		}

		[CLSCompliant (false)]
		unsafe public static ushort ToUInt16 (byte[] value, int startIndex)
		{
			ushort ret;

			PutBytes ((byte *) &ret, value, startIndex, 2);

			return ret;
		}

		[CLSCompliant (false)]
		unsafe public static uint ToUInt32 (byte[] value, int startIndex)
		{
			uint ret;

			PutBytes ((byte *) &ret, value, startIndex, 4);

			return ret;
		}

		[CLSCompliant (false)]
		unsafe public static ulong ToUInt64 (byte[] value, int startIndex)
		{
			ulong ret;

			PutBytes ((byte *) &ret, value, startIndex, 8);

			return ret;
		}

		unsafe public static float ToSingle (byte[] value, int startIndex)
		{
			float ret;

			PutBytes ((byte *) &ret, value, startIndex, 4);

			return ret;
		}

		unsafe public static double ToDouble (byte[] value, int startIndex)
		{
			double ret;

			PutBytes ((byte *) &ret, value, startIndex, 8);

			return ret;
		}

		public static string ToString (byte[] value)
		{
			if (value == null) {
				throw new ArgumentNullException ("value");
			}

			return ToString (value, 0, value.Length);
		}

		public static string ToString (byte[] value, int startIndex)
		{
			if (value == null)
				throw new ArgumentNullException ("value");
			if (startIndex < 0 || startIndex > value.Length - 1) 
				throw new ArgumentOutOfRangeException ("startIndex");

			return ToString (value, startIndex, value.Length - startIndex);
		}

		public static string ToString (byte[] value, int startIndex, int length)
		{
			if (value == null) {
				throw new ArgumentNullException ("value");
			}

			// The 4th and last clause (start_index >= value.Length)
			// was added as a small fix to a very obscure bug.
			// It makes a small difference when start_index is
			// outside the range and length==0. 
			if (startIndex < 0 || length < 0 || startIndex >= value.Length)
				throw new ArgumentOutOfRangeException ("startIndex");
			// note: re-ordered to avoid possible integer overflow
			if (startIndex > value.Length - length)
				throw new ArgumentException ("startIndex + length > value.Length");

			string ret = "";
			int end = startIndex + length;

			for (int i = startIndex; i < end; i++) {
				if (i > startIndex)
					ret = ret + '-';
				
				char high = (char)((value[i] >> 4) & 0x0f);
				char low = (char)(value[i] & 0x0f);

				if (high < 10) 
					high += '0';
				else {
					high -= (char) 10;
					high += 'A';
				}

				if (low < 10)
					low += '0';
				else {
					low -= (char) 10;
					low += 'A';
				}
				ret = ret + high + low;
			}

			return ret;
		}
	}
}