Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .ToBytes(UInt64 value, Byte[] array, Int32 offset);
- .ToBytes(UInt16 value, Byte[] array, Int32 offset);
- public unsafe static byte[] GetBytes(long value)
- {
- byte[] array = new byte[8];
- fixed (byte* ptr = array)
- {
- *(long*)ptr = value;
- }
- return array;
- }
- static unsafe void ToBytes(ulong value, byte[] array, int offset)
- {
- fixed (byte* ptr = &array[offset])
- *(ulong*)ptr = value;
- }
- byte[] array = new byte[9];
- ToBytes(0x1122334455667788, array, 1);
- static void ToBytes(ulong value, byte[] array, int offset)
- {
- byte[] valueBytes = BitConverter.GetBytes(value);
- Array.Copy(valueBytes, 0, array, offset, valueBytes.Length);
- }
- static void ToBytes(ulong value, byte[] array, int offset)
- {
- for (int i = 0; i < 8; i++)
- {
- array[offset + i] = (byte)value;
- value >>= 8;
- }
- }
- static void ToBytes(ulong value, byte[] array, int offset) {
- unchecked {
- array[offset + 0] = (byte)(value >> (8*7));
- array[offset + 1] = (byte)(value >> (8*6));
- array[offset + 2] = (byte)(value >> (8*5));
- array[offset + 3] = (byte)(value >> (8*4));
- //...
- }
- }
- static class Extensions
- {
- public static void ToBytes(this int n, byte[] destination, int offset)
- {
- var data = BitConverter.GetBytes(n);
- Array.Copy(data, 0, destination, offset, data.Length);
- }
- }
- byte[] destination = new byte[10];
- 123.ToBytes(destination,0);
- public static void ToBytes(ulong value, byte[] array, int offset)
- {
- GCHandle handle = GCHandle.Alloc(value, GCHandleType.Pinned);
- try
- {
- Marshal.Copy(handle.AddrOfPinnedObject(), array, offset, 8);
- }
- finally
- {
- handle.Free();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement