• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lduchosal / ipnetwork / 581

02 Jun 2023 08:29AM UTC coverage: 94.615%. Remained the same
581

push

appveyor

web-flow
Merge pull request #271 from lduchosal/dependabot/nuget/Microsoft.CodeCoverage-17.6.1

Build(deps): Bump Microsoft.CodeCoverage from 17.6.0 to 17.6.1

1599 of 1690 relevant lines covered (94.62%)

1185099.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/System.Net.IPNetwork/BigIntegerExtensions.cs
1
// <copyright file="BigIntegerExtensions.cs" company="IPNetwork">
2
// Copyright (c) IPNetwork. All rights reserved.
3
// </copyright>
4

5
namespace System.Net
6
{
7
    using System;
8
    using System.Numerics;
9
    using System.Text;
10

11
    /// <summary>
12
    /// Extension methods to convert <see cref="System.Numerics.BigInteger"/>
13
    /// instances to hexadecimal, octal, and binary strings.
14
    /// </summary>
15
    public static class BigIntegerExtensions
16
    {
17
        /// <summary>
18
        /// Converts a <see cref="BigInteger"/> to a binary string.
19
        /// </summary>
20
        /// <param name="bigint">A <see cref="BigInteger"/>.</param>
21
        /// <returns>
22
        /// A <see cref="string"/> containing a binary
23
        /// representation of the supplied <see cref="BigInteger"/>.
24
        /// </returns>
25
        public static string ToBinaryString(this BigInteger bigint)
26
        {
6,000,940✔
27
            byte[] bytes = bigint.ToByteArray();
6,000,940✔
28
            int idx = bytes.Length - 1;
6,000,940✔
29

30
            // Create a StringBuilder having appropriate capacity.
31
            var base2 = new StringBuilder(bytes.Length * 8);
6,000,940✔
32

33
            // Convert first byte to binary.
34
            string binary = Convert.ToString(bytes[idx], 2);
6,000,940✔
35

36
            // Ensure leading zero exists if value is positive.
37
            if (binary[0] != '0' && bigint.Sign == 1)
6,000,940✔
38
            {
1✔
39
                base2.Append('0');
1✔
40
            }
1✔
41

42
            // Append binary string to StringBuilder.
43
            base2.Append(binary);
6,000,940✔
44

45
            // Convert remaining bytes adding leading zeros.
46
            for (idx--; idx >= 0; idx--)
60,016,600✔
47
            {
24,007,360✔
48
                base2.Append(Convert.ToString(bytes[idx], 2).PadLeft(8, '0'));
24,007,360✔
49
            }
24,007,360✔
50

51
            return base2.ToString();
6,000,940✔
52
        }
6,000,940✔
53

54
        /// <summary>
55
        /// Converts a <see cref="BigInteger"/> to a hexadecimal string.
56
        /// </summary>
57
        /// <param name="bigint">A <see cref="BigInteger"/>.</param>
58
        /// <returns>
59
        /// A <see cref="string"/> containing a hexadecimal
60
        /// representation of the supplied <see cref="BigInteger"/>.
61
        /// </returns>
62
        public static string ToHexadecimalString(this BigInteger bigint)
63
        {
1✔
64
            return bigint.ToString("X");
1✔
65
        }
1✔
66

67
        /// <summary>
68
        /// Converts a <see cref="BigInteger"/> to a octal string.
69
        /// </summary>
70
        /// <param name="bigint">A <see cref="BigInteger"/>.</param>
71
        /// <returns>
72
        /// A <see cref="string"/> containing an octal
73
        /// representation of the supplied <see cref="BigInteger"/>.
74
        /// </returns>
75
        public static string ToOctalString(this BigInteger bigint)
76
        {
5✔
77
            byte[] bytes = bigint.ToByteArray();
5✔
78
            int idx = bytes.Length - 1;
5✔
79

80
            // Create a StringBuilder having appropriate capacity.
81
            var base8 = new StringBuilder(((bytes.Length / 3) + 1) * 8);
5✔
82

83
            // Calculate how many bytes are extra when byte array is split
84
            // into three-byte (24-bit) chunks.
85
            int extra = bytes.Length % 3;
5✔
86

87
            // If no bytes are extra, use three bytes for first chunk.
88
            if (extra == 0)
5✔
89
            {
1✔
90
                extra = 3;
1✔
91
            }
1✔
92

93
            // Convert first chunk (24-bits) to integer value.
94
            int int24 = 0;
5✔
95
            for (; extra != 0; extra--)
21✔
96
            {
8✔
97
                int24 <<= 8;
8✔
98
                int24 += bytes[idx--];
8✔
99
            }
8✔
100

101
            // Convert 24-bit integer to octal without adding leading zeros.
102
            string octal = Convert.ToString(int24, 8);
5✔
103

104
            // Ensure leading zero exists if value is positive.
105
            if (octal[0] != '0')
5✔
106
            {
5✔
107
                if (bigint.Sign == 1)
5✔
108
                {
3✔
109
                    base8.Append('0');
3✔
110
                }
3✔
111
            }
5✔
112

113
            // Append first converted chunk to StringBuilder.
114
            base8.Append(octal);
5✔
115

116
            // Convert remaining 24-bit chunks, adding leading zeros.
117
            for (; idx >= 0; idx -= 3)
7✔
118
            {
1✔
119
                int24 = (bytes[idx] << 16) + (bytes[idx - 1] << 8) + bytes[idx - 2];
1✔
120
                base8.Append(Convert.ToString(int24, 8).PadLeft(8, '0'));
1✔
121
            }
1✔
122

123
            return base8.ToString();
5✔
124
        }
5✔
125

126
        /// <summary>
127
        ///
128
        /// Reverse a Positive BigInteger ONLY
129
        /// Bitwise ~ operator
130
        ///
131
        /// Input  : FF FF FF FF
132
        /// Width  : 4
133
        /// Result : 00 00 00 00
134
        ///
135
        ///
136
        /// Input  : 00 00 00 00
137
        /// Width  : 4
138
        /// Result : FF FF FF FF
139
        ///
140
        /// Input  : FF FF FF FF
141
        /// Width  : 8
142
        /// Result : FF FF FF FF 00 00 00 00
143
        ///
144
        ///
145
        /// Input  : 00 00 00 00
146
        /// Width  : 8
147
        /// Result : FF FF FF FF FF FF FF FF.
148
        ///
149
        /// </summary>
150
        /// <param name="input">The positive number to bitwise reverse.</param>
151
        /// <param name="width">The width of the parameter.
152
        /// </param>
153
        /// <returns>A number representing the input bitwise reversed.</returns>
154
        public static BigInteger PositiveReverse(this BigInteger input, int width)
155
        {
133,913✔
156
            byte[] bytes = input.ToByteArray();
133,913✔
157
            int length = width + 1;
133,913✔
158

159
            // if the byte array is same size as output, we'll perform the operations in place
160
            byte[] output = bytes.Length != length ? new byte[length] : bytes;
133,913✔
161

162
            // invert all of the source bytes
163
            for (int i = 0; i < bytes.Length - 1; i++)
1,365,066✔
164
            {
548,620✔
165
                output[i] = (byte)~bytes[i];
548,620✔
166
            }
548,620✔
167

168
            // invert the remainder of the output buffer
169
            for (int i = bytes.Length - 1; i < output.Length - 1; i++)
268,058✔
170
            {
116✔
171
                output[i] = byte.MaxValue;
116✔
172
            }
116✔
173

174
            // ensure output value is positive and return
175
            output[output.Length - 1] = 0;
133,913✔
176
            return new BigInteger(output);
133,913✔
177
        }
133,913✔
178
    }
179
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc