Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
BitConverter.cs
Go to the documentation of this file.
6
7namespace System;
8
9public static class BitConverter
10{
11 [Intrinsic]
12 public static readonly bool IsLittleEndian = true;
13
14 public static byte[] GetBytes(bool value)
15 {
16 return new byte[1] { (byte)(value ? 1 : 0) };
17 }
18
19 public static bool TryWriteBytes(Span<byte> destination, bool value)
20 {
21 if (destination.Length < 1)
22 {
23 return false;
24 }
25 Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), (byte)(value ? 1 : 0));
26 return true;
27 }
28
29 public static byte[] GetBytes(char value)
30 {
31 byte[] array = new byte[2];
32 Unsafe.As<byte, char>(ref array[0]) = value;
33 return array;
34 }
35
36 public static bool TryWriteBytes(Span<byte> destination, char value)
37 {
38 if (destination.Length < 2)
39 {
40 return false;
41 }
42 Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value);
43 return true;
44 }
45
46 public static byte[] GetBytes(short value)
47 {
48 byte[] array = new byte[2];
49 Unsafe.As<byte, short>(ref array[0]) = value;
50 return array;
51 }
52
53 public static bool TryWriteBytes(Span<byte> destination, short value)
54 {
55 if (destination.Length < 2)
56 {
57 return false;
58 }
59 Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value);
60 return true;
61 }
62
63 public static byte[] GetBytes(int value)
64 {
65 byte[] array = new byte[4];
66 Unsafe.As<byte, int>(ref array[0]) = value;
67 return array;
68 }
69
70 public static bool TryWriteBytes(Span<byte> destination, int value)
71 {
72 if (destination.Length < 4)
73 {
74 return false;
75 }
76 Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value);
77 return true;
78 }
79
80 public static byte[] GetBytes(long value)
81 {
82 byte[] array = new byte[8];
83 Unsafe.As<byte, long>(ref array[0]) = value;
84 return array;
85 }
86
87 public static bool TryWriteBytes(Span<byte> destination, long value)
88 {
89 if (destination.Length < 8)
90 {
91 return false;
92 }
93 Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value);
94 return true;
95 }
96
97 [CLSCompliant(false)]
98 public static byte[] GetBytes(ushort value)
99 {
100 byte[] array = new byte[2];
101 Unsafe.As<byte, ushort>(ref array[0]) = value;
102 return array;
103 }
104
105 [CLSCompliant(false)]
106 public static bool TryWriteBytes(Span<byte> destination, ushort value)
107 {
108 if (destination.Length < 2)
109 {
110 return false;
111 }
112 Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value);
113 return true;
114 }
115
116 [CLSCompliant(false)]
117 public static byte[] GetBytes(uint value)
118 {
119 byte[] array = new byte[4];
120 Unsafe.As<byte, uint>(ref array[0]) = value;
121 return array;
122 }
123
124 [CLSCompliant(false)]
125 public static bool TryWriteBytes(Span<byte> destination, uint value)
126 {
127 if (destination.Length < 4)
128 {
129 return false;
130 }
131 Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value);
132 return true;
133 }
134
135 [CLSCompliant(false)]
136 public static byte[] GetBytes(ulong value)
137 {
138 byte[] array = new byte[8];
139 Unsafe.As<byte, ulong>(ref array[0]) = value;
140 return array;
141 }
142
143 [CLSCompliant(false)]
144 public static bool TryWriteBytes(Span<byte> destination, ulong value)
145 {
146 if (destination.Length < 8)
147 {
148 return false;
149 }
150 Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value);
151 return true;
152 }
153
154 public unsafe static byte[] GetBytes(Half value)
155 {
156 byte[] array = new byte[sizeof(Half)];
157 Unsafe.As<byte, Half>(ref array[0]) = value;
158 return array;
159 }
160
161 public unsafe static bool TryWriteBytes(Span<byte> destination, Half value)
162 {
163 if (destination.Length < sizeof(Half))
164 {
165 return false;
166 }
167 Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value);
168 return true;
169 }
170
171 public static byte[] GetBytes(float value)
172 {
173 byte[] array = new byte[4];
174 Unsafe.As<byte, float>(ref array[0]) = value;
175 return array;
176 }
177
178 public static bool TryWriteBytes(Span<byte> destination, float value)
179 {
180 if (destination.Length < 4)
181 {
182 return false;
183 }
184 Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value);
185 return true;
186 }
187
188 public static byte[] GetBytes(double value)
189 {
190 byte[] array = new byte[8];
191 Unsafe.As<byte, double>(ref array[0]) = value;
192 return array;
193 }
194
195 public static bool TryWriteBytes(Span<byte> destination, double value)
196 {
197 if (destination.Length < 8)
198 {
199 return false;
200 }
201 Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value);
202 return true;
203 }
204
205 public static char ToChar(byte[] value, int startIndex)
206 {
207 return (char)ToInt16(value, startIndex);
208 }
209
210 [MethodImpl(MethodImplOptions.AggressiveInlining)]
211 public static char ToChar(ReadOnlySpan<byte> value)
212 {
213 if (value.Length < 2)
214 {
216 }
217 return Unsafe.ReadUnaligned<char>(ref MemoryMarshal.GetReference(value));
218 }
219
220 public static short ToInt16(byte[] value, int startIndex)
221 {
222 if (value == null)
223 {
225 }
226 if ((uint)startIndex >= (uint)value.Length)
227 {
229 }
230 if (startIndex > value.Length - 2)
231 {
233 }
234 return Unsafe.ReadUnaligned<short>(ref value[startIndex]);
235 }
236
237 [MethodImpl(MethodImplOptions.AggressiveInlining)]
238 public static short ToInt16(ReadOnlySpan<byte> value)
239 {
240 if (value.Length < 2)
241 {
243 }
244 return Unsafe.ReadUnaligned<short>(ref MemoryMarshal.GetReference(value));
245 }
246
247 public static int ToInt32(byte[] value, int startIndex)
248 {
249 if (value == null)
250 {
252 }
253 if ((uint)startIndex >= (uint)value.Length)
254 {
256 }
257 if (startIndex > value.Length - 4)
258 {
260 }
261 return Unsafe.ReadUnaligned<int>(ref value[startIndex]);
262 }
263
264 [MethodImpl(MethodImplOptions.AggressiveInlining)]
266 {
267 if (value.Length < 4)
268 {
270 }
271 return Unsafe.ReadUnaligned<int>(ref MemoryMarshal.GetReference(value));
272 }
273
274 public static long ToInt64(byte[] value, int startIndex)
275 {
276 if (value == null)
277 {
279 }
280 if ((uint)startIndex >= (uint)value.Length)
281 {
283 }
284 if (startIndex > value.Length - 8)
285 {
287 }
288 return Unsafe.ReadUnaligned<long>(ref value[startIndex]);
289 }
290
291 [MethodImpl(MethodImplOptions.AggressiveInlining)]
292 public static long ToInt64(ReadOnlySpan<byte> value)
293 {
294 if (value.Length < 8)
295 {
297 }
298 return Unsafe.ReadUnaligned<long>(ref MemoryMarshal.GetReference(value));
299 }
300
301 [CLSCompliant(false)]
302 public static ushort ToUInt16(byte[] value, int startIndex)
303 {
304 return (ushort)ToInt16(value, startIndex);
305 }
306
307 [MethodImpl(MethodImplOptions.AggressiveInlining)]
308 [CLSCompliant(false)]
309 public static ushort ToUInt16(ReadOnlySpan<byte> value)
310 {
311 if (value.Length < 2)
312 {
314 }
315 return Unsafe.ReadUnaligned<ushort>(ref MemoryMarshal.GetReference(value));
316 }
317
318 [CLSCompliant(false)]
319 public static uint ToUInt32(byte[] value, int startIndex)
320 {
321 return (uint)ToInt32(value, startIndex);
322 }
323
324 [MethodImpl(MethodImplOptions.AggressiveInlining)]
325 [CLSCompliant(false)]
327 {
328 if (value.Length < 4)
329 {
331 }
332 return Unsafe.ReadUnaligned<uint>(ref MemoryMarshal.GetReference(value));
333 }
334
335 [CLSCompliant(false)]
336 public static ulong ToUInt64(byte[] value, int startIndex)
337 {
338 return (ulong)ToInt64(value, startIndex);
339 }
340
341 [MethodImpl(MethodImplOptions.AggressiveInlining)]
342 [CLSCompliant(false)]
343 public static ulong ToUInt64(ReadOnlySpan<byte> value)
344 {
345 if (value.Length < 8)
346 {
348 }
349 return Unsafe.ReadUnaligned<ulong>(ref MemoryMarshal.GetReference(value));
350 }
351
352 public static Half ToHalf(byte[] value, int startIndex)
353 {
355 }
356
357 [MethodImpl(MethodImplOptions.AggressiveInlining)]
358 public unsafe static Half ToHalf(ReadOnlySpan<byte> value)
359 {
360 if (value.Length < sizeof(Half))
361 {
363 }
364 return Unsafe.ReadUnaligned<Half>(ref MemoryMarshal.GetReference(value));
365 }
366
367 public static float ToSingle(byte[] value, int startIndex)
368 {
370 }
371
372 [MethodImpl(MethodImplOptions.AggressiveInlining)]
373 public static float ToSingle(ReadOnlySpan<byte> value)
374 {
375 if (value.Length < 4)
376 {
378 }
379 return Unsafe.ReadUnaligned<float>(ref MemoryMarshal.GetReference(value));
380 }
381
382 public static double ToDouble(byte[] value, int startIndex)
383 {
385 }
386
387 [MethodImpl(MethodImplOptions.AggressiveInlining)]
388 public static double ToDouble(ReadOnlySpan<byte> value)
389 {
390 if (value.Length < 8)
391 {
393 }
394 return Unsafe.ReadUnaligned<double>(ref MemoryMarshal.GetReference(value));
395 }
396
397 public static string ToString(byte[] value, int startIndex, int length)
398 {
399 if (value == null)
400 {
402 }
403 if (startIndex < 0 || (startIndex >= value.Length && startIndex > 0))
404 {
406 }
407 if (length < 0)
408 {
410 }
411 if (startIndex > value.Length - length)
412 {
414 }
415 if (length == 0)
416 {
417 return string.Empty;
418 }
419 if (length > 715827882)
420 {
422 }
423 return string.Create(length * 3 - 1, (value, startIndex, length), delegate(Span<char> dst, (byte[] value, int startIndex, int length) state)
424 {
425 ReadOnlySpan<byte> readOnlySpan = new ReadOnlySpan<byte>(state.value, state.startIndex, state.length);
426 int num = 0;
427 int num2 = 0;
428 byte b = readOnlySpan[num++];
429 dst[num2++] = HexConverter.ToCharUpper(b >> 4);
430 dst[num2++] = HexConverter.ToCharUpper(b);
431 while (num < readOnlySpan.Length)
432 {
433 b = readOnlySpan[num++];
434 dst[num2++] = '-';
435 dst[num2++] = HexConverter.ToCharUpper(b >> 4);
436 dst[num2++] = HexConverter.ToCharUpper(b);
437 }
438 });
439 }
440
441 public static string ToString(byte[] value)
442 {
443 if (value == null)
444 {
446 }
447 return ToString(value, 0, value.Length);
448 }
449
450 public static string ToString(byte[] value, int startIndex)
451 {
452 if (value == null)
453 {
455 }
456 return ToString(value, startIndex, value.Length - startIndex);
457 }
458
459 public static bool ToBoolean(byte[] value, int startIndex)
460 {
461 if (value == null)
462 {
464 }
465 if (startIndex < 0)
466 {
468 }
469 if (startIndex > value.Length - 1)
470 {
472 }
473 return value[startIndex] != 0;
474 }
475
476 [MethodImpl(MethodImplOptions.AggressiveInlining)]
478 {
479 if (value.Length < 1)
480 {
482 }
483 return Unsafe.ReadUnaligned<byte>(ref MemoryMarshal.GetReference(value)) != 0;
484 }
485
486 [MethodImpl(MethodImplOptions.AggressiveInlining)]
487 public unsafe static long DoubleToInt64Bits(double value)
488 {
489 if (Sse2.X64.IsSupported)
490 {
492 return Sse2.X64.ConvertToInt64(value2);
493 }
494 return *(long*)(&value);
495 }
496
497 [MethodImpl(MethodImplOptions.AggressiveInlining)]
498 public unsafe static double Int64BitsToDouble(long value)
499 {
500 if (Sse2.X64.IsSupported)
501 {
503 return vector.ToScalar();
504 }
505 return *(double*)(&value);
506 }
507
508 [MethodImpl(MethodImplOptions.AggressiveInlining)]
509 public unsafe static int SingleToInt32Bits(float value)
510 {
511 if (Sse2.IsSupported)
512 {
514 return Sse2.ConvertToInt32(value2);
515 }
516 return *(int*)(&value);
517 }
518
519 [MethodImpl(MethodImplOptions.AggressiveInlining)]
520 public unsafe static float Int32BitsToSingle(int value)
521 {
522 if (Sse2.IsSupported)
523 {
525 return vector.ToScalar();
526 }
527 return *(float*)(&value);
528 }
529
530 [MethodImpl(MethodImplOptions.AggressiveInlining)]
531 public unsafe static short HalfToInt16Bits(Half value)
532 {
533 return *(short*)(&value);
534 }
535
536 [MethodImpl(MethodImplOptions.AggressiveInlining)]
537 public unsafe static Half Int16BitsToHalf(short value)
538 {
539 return *(Half*)(&value);
540 }
541
542 [MethodImpl(MethodImplOptions.AggressiveInlining)]
543 [CLSCompliant(false)]
544 public static ulong DoubleToUInt64Bits(double value)
545 {
546 return (ulong)DoubleToInt64Bits(value);
547 }
548
549 [MethodImpl(MethodImplOptions.AggressiveInlining)]
550 [CLSCompliant(false)]
551 public static double UInt64BitsToDouble(ulong value)
552 {
553 return Int64BitsToDouble((long)value);
554 }
555
556 [MethodImpl(MethodImplOptions.AggressiveInlining)]
557 [CLSCompliant(false)]
558 public static uint SingleToUInt32Bits(float value)
559 {
560 return (uint)SingleToInt32Bits(value);
561 }
562
563 [MethodImpl(MethodImplOptions.AggressiveInlining)]
564 [CLSCompliant(false)]
565 public static float UInt32BitsToSingle(uint value)
566 {
567 return Int32BitsToSingle((int)value);
568 }
569
570 [MethodImpl(MethodImplOptions.AggressiveInlining)]
571 [CLSCompliant(false)]
572 public static ushort HalfToUInt16Bits(Half value)
573 {
574 return (ushort)HalfToInt16Bits(value);
575 }
576
577 [MethodImpl(MethodImplOptions.AggressiveInlining)]
578 [CLSCompliant(false)]
579 public static Half UInt16BitsToHalf(ushort value)
580 {
581 return Int16BitsToHalf((short)value);
582 }
583}
static bool TryWriteBytes(Span< byte > destination, long value)
static byte[] GetBytes(short value)
static byte[] GetBytes(float value)
static Half UInt16BitsToHalf(ushort value)
static unsafe float Int32BitsToSingle(int value)
static unsafe Half ToHalf(ReadOnlySpan< byte > value)
static bool TryWriteBytes(Span< byte > destination, ushort value)
static unsafe bool TryWriteBytes(Span< byte > destination, Half value)
static byte[] GetBytes(long value)
static bool TryWriteBytes(Span< byte > destination, int value)
static byte[] GetBytes(char value)
static char ToChar(byte[] value, int startIndex)
static string ToString(byte[] value, int startIndex, int length)
static ushort HalfToUInt16Bits(Half value)
static bool TryWriteBytes(Span< byte > destination, bool value)
static unsafe int SingleToInt32Bits(float value)
static bool TryWriteBytes(Span< byte > destination, char value)
static short ToInt16(ReadOnlySpan< byte > value)
static unsafe double Int64BitsToDouble(long value)
static bool TryWriteBytes(Span< byte > destination, short value)
static long ToInt64(byte[] value, int startIndex)
static ulong DoubleToUInt64Bits(double value)
static byte[] GetBytes(ulong value)
static unsafe long DoubleToInt64Bits(double value)
static uint ToUInt32(byte[] value, int startIndex)
static bool TryWriteBytes(Span< byte > destination, ulong value)
static bool ToBoolean(ReadOnlySpan< byte > value)
static float ToSingle(ReadOnlySpan< byte > value)
static float ToSingle(byte[] value, int startIndex)
static short ToInt16(byte[] value, int startIndex)
static bool TryWriteBytes(Span< byte > destination, double value)
static bool TryWriteBytes(Span< byte > destination, uint value)
static unsafe Half Int16BitsToHalf(short value)
static byte[] GetBytes(uint value)
static readonly bool IsLittleEndian
static ulong ToUInt64(ReadOnlySpan< byte > value)
static byte[] GetBytes(int value)
static int ToInt32(ReadOnlySpan< byte > value)
static string ToString(byte[] value, int startIndex)
static uint ToUInt32(ReadOnlySpan< byte > value)
static Half ToHalf(byte[] value, int startIndex)
static byte[] GetBytes(ushort value)
static unsafe byte[] GetBytes(Half value)
static double ToDouble(byte[] value, int startIndex)
static char ToChar(ReadOnlySpan< byte > value)
static double UInt64BitsToDouble(ulong value)
static ulong ToUInt64(byte[] value, int startIndex)
static ushort ToUInt16(byte[] value, int startIndex)
static double ToDouble(ReadOnlySpan< byte > value)
static string ToString(byte[] value)
static byte[] GetBytes(bool value)
static long ToInt64(ReadOnlySpan< byte > value)
static int ToInt32(byte[] value, int startIndex)
static bool ToBoolean(byte[] value, int startIndex)
static unsafe short HalfToInt16Bits(Half value)
static bool TryWriteBytes(Span< byte > destination, float value)
static uint SingleToUInt32Bits(float value)
static float UInt32BitsToSingle(uint value)
static byte[] GetBytes(double value)
static ushort ToUInt16(ReadOnlySpan< byte > value)
static char ToCharUpper(int value)
static unsafe Vector128< byte > CreateScalarUnsafe(byte value)
Definition Vector128.cs:829
static long ConvertToInt64(Vector128< double > value)
Definition Sse2.cs:14
static new bool IsSupported
Definition Sse2.cs:60
static int ConvertToInt32(Vector128< double > value)
Definition Sse2.cs:502
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string ArgumentOutOfRange_LengthTooLarge
Definition SR.cs:1052
static string ArgumentOutOfRange_GenericPositive
Definition SR.cs:1018
Definition SR.cs:7
static void ThrowArgumentOutOfRangeException(System.ExceptionArgument argument)
static void ThrowArgumentNullException(string name)
static void ThrowArgumentException(ExceptionResource resource)