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

BitExtensions.cs « Extensions « UVtools.Core - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 252062ce8826a934d7ecff295d7cb101e2488189 (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
/*
 *                     GNU AFFERO GENERAL PUBLIC LICENSE
 *                       Version 3, 19 November 2007
 *  Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
 *  Everyone is permitted to copy and distribute verbatim copies
 *  of this license document, but changing it is not allowed.
 */
namespace UVtools.Core.Extensions
{
    public static class BitExtensions
    {
        public static ushort ToUShortLittleEndian(byte byte1, byte byte2) => (ushort)(byte1 + (byte2 << 8));
        public static ushort ToUShortBigEndian(byte byte1, byte byte2) => (ushort)((byte1 << 8) + byte2);

        public static ushort ToUShortLittleEndian(byte[] buffer, int offset = 0)
            => (ushort)(buffer[offset] + (buffer[offset+1] << 8));
        public static ushort ToUShortBigEndian(byte[] buffer, int offset = 0)
            => (ushort)((buffer[offset] << 8) + buffer[offset+1]);

        public static uint ToUIntLittleEndian(byte byte1, byte byte2, byte byte3, byte byte4) 
            => (uint)(byte1 + (byte2 << 8) + (byte3 << 16) + (byte4 << 24));
        public static uint ToUIntBigEndian(byte byte1, byte byte2, byte byte3, byte byte4) 
            => (uint)((byte1 << 24) + (byte1 << 16) + (byte1 << 8) + byte2);

        public static uint ToUIntLittleEndian(byte[] buffer, int offset = 0)
            => (uint)(buffer[offset] + (buffer[offset + 1] << 8) + (buffer[offset + 2] << 16) + (buffer[offset + 3] << 24));
        public static uint ToUIntBigEndian(byte[] buffer, int offset = 0)
            => (uint)((buffer[offset] << 24) + (buffer[offset+1] << 16) + (buffer[offset+2] << 8) + buffer[offset+3]);

        public static byte[] ToBytesLittleEndian(ushort value)
        {
            var bytes = new byte[2];
            ToBytesLittleEndian(value, bytes);
            return bytes;
        }

        public static void ToBytesLittleEndian(ushort value, byte[] buffer, uint offset = 0)
        {
            buffer[offset] = (byte)value;
            buffer[offset + 1] = (byte)(value >> 8);
        }

        public static byte[] ToBytesBigEndian(ushort value)
        {
            var bytes = new byte[2];
            ToBytesBigEndian(value, bytes);
            return bytes;
        }

        public static void ToBytesBigEndian(ushort value, byte[] buffer, uint offset = 0)
        {
            buffer[offset] = (byte)(value >> 8);
            buffer[offset + 1] = (byte)value;
        }

        public static byte[] ToBytesLittleEndian(uint value)
        {
            var bytes = new byte[4];
            ToBytesLittleEndian(value, bytes);
            return bytes;
        }

        public static void ToBytesLittleEndian(uint value, byte[] buffer, uint offset = 0)
        {
            buffer[offset] = (byte)value;
            buffer[offset + 1] = (byte)(value >> 8);
            buffer[offset + 2] = (byte)(value >> 16);
            buffer[offset + 3] = (byte)(value >> 24);
        }

        public static byte[] ToBytesBigEndian(uint value)
        {
            var bytes = new byte[4];
            ToBytesBigEndian(value, bytes);
            return bytes;
        }

        public static void ToBytesBigEndian(uint value, byte[] buffer, uint offset = 0)
        {
            buffer[offset] = (byte)(value >> 24);
            buffer[offset + 1] = (byte)(value >> 16);
            buffer[offset + 2] = (byte)(value >> 8);
            buffer[offset + 3] = (byte)value;
        }

        public static byte[] ToBytesLittleEndian(int value)
        {
            var bytes = new byte[4];
            ToBytesLittleEndian(value, bytes);
            return bytes;
        }

        public static void ToBytesLittleEndian(int value, byte[] buffer, uint offset = 0)
        {
            buffer[offset] = (byte)value;
            buffer[offset + 1] = (byte)(value >> 8);
            buffer[offset + 2] = (byte)(value >> 16);
            buffer[offset + 3] = (byte)(value >> 24);
        }

        public static byte[] ToBytesBigEndian(int value)
        {
            var bytes = new byte[4];
            ToBytesBigEndian(value, bytes);
            return bytes;
        }

        public static void ToBytesBigEndian(int value, byte[] buffer, uint offset = 0)
        {
            buffer[offset] = (byte)(value >> 24);
            buffer[offset + 1] = (byte)(value >> 16);
            buffer[offset + 2] = (byte)(value >> 8);
            buffer[offset + 3] = (byte)value;
        }


    }
}