1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4

5
namespace Libplanet.Stun
6
{
7
    internal static class BytesConvertExtensions
8
    {
9
        public static byte[] ToBytes(this ushort value)
10 2
        {
11 2
            var rv = new byte[sizeof(ushort)];
12

13 2
            rv[0] = (byte)((0xff00 & value) >> 8);
14 2
            rv[1] = (byte)(0x00ff & value);
15

16 2
            return rv;
17 2
        }
18

19
        public static byte[] ToBytes(this uint value)
20 2
        {
21 2
            var rv = new byte[sizeof(uint)];
22

23 2
            rv[0] = (byte)((0xff000000 & value) >> 24);
24 2
            rv[1] = (byte)((0x00ff0000 & value) >> 16);
25 2
            rv[2] = (byte)((0x0000ff00 & value) >> 8);
26 2
            rv[3] = (byte)(0x000000ff & value);
27

28 2
            return rv;
29 2
        }
30

31
        public static ushort ToUShort(this IEnumerable<byte> bytes)
32 2
        {
33 2
            byte[] bs = bytes.ToArray();
34

35 2
            if (bs.Length != sizeof(ushort))
36 0
            {
37 0
                throw new ArgumentOutOfRangeException(nameof(bytes));
38
            }
39

40 2
            ushort rv = bs[0];
41 2
            rv = (ushort)(rv << 8);
42 2
            rv |= bs[1];
43

44 2
            return rv;
45 2
        }
46

47
        public static uint ToUInt(this IEnumerable<byte> bytes)
48 2
        {
49 2
            byte[] bs = bytes.ToArray();
50

51 2
            if (bs.Length != sizeof(uint))
52 0
            {
53 0
                throw new ArgumentOutOfRangeException(nameof(bytes));
54
            }
55

56 2
            uint rv = bs[0];
57 2
            rv = rv << 8;
58 2
            rv |= bs[1];
59 2
            rv = rv << 8;
60 2
            rv |= bs[2];
61 2
            rv = rv << 8;
62 2
            rv |= bs[3];
63

64 2
            return rv;
65 2
        }
66
    }
67
}

Read our documentation on viewing source code .

Loading