-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathBitConverter.cs
40 lines (37 loc) · 1.39 KB
/
BitConverter.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UEx
{
public class BitConverter
{
/// <summary>
/// copy the bytes of the specified int into the buffer
/// </summary>
/// <param name="value"></param>
/// <param name="buffer"></param>
/// <param name="offset"></param>
/// <exception cref="IndexOutOfRangeException"></exception>
public static unsafe void CopyBytes(int value, byte[] buffer, int offset)
{
// Here should be a range check. For example:
if (offset + sizeof(int) > buffer.Length) throw new IndexOutOfRangeException();
fixed (byte* numPtr = &buffer[offset])
*(int*) numPtr = value;
}
/// <summary>
/// copy the bytes of the specified float into the buffer
/// </summary>
/// <param name="value"></param>
/// <param name="buffer"></param>
/// <param name="offset"></param>
public static unsafe void CopyBytes(float value, byte[] buffer, int offset)
{
// Here should be a range check. For example:
if (offset + sizeof(float) > buffer.Length) throw new IndexOutOfRangeException();
fixed (byte* numPtr = &buffer[offset])
*(float*)numPtr = value;
}
}
}