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

lduchosal / ipnetwork / 960

15 May 2026 07:18AM UTC coverage: 0.0% (-94.1%) from 94.053%
960

Pull #411

appveyor

web-flow
Merge ef6b054ce into 4cec191ae
Pull Request #411: Bump MSTest.TestFramework from 4.2.2 to 4.2.3

0 of 2438 relevant lines covered (0.0%)

0.0 hits per line

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

0.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
[CLSCompliant(true)]
16
public static class BigIntegerExtensions
17
{
18
    /// <summary>
19
    /// Converts a <see cref="BigInteger"/> to a binary string.
20
    /// </summary>
21
    /// <param name="bigint">A <see cref="BigInteger"/>.</param>
22
    /// <returns>
23
    /// A <see cref="string"/> containing a binary
24
    /// representation of the supplied <see cref="BigInteger"/>.
25
    /// </returns>
26
    public static string ToBinaryString(this BigInteger bigint)
27
    {
×
28
            byte[] bytes = bigint.ToByteArray();
×
29
            int idx = bytes.Length - 1;
×
30

31
            // Create a StringBuilder having appropriate capacity.
32
            var base2 = new StringBuilder(bytes.Length * 8);
×
33

34
            // Convert first byte to binary.
35
            string binary = Convert.ToString(bytes[idx], 2);
×
36

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

43
            // Append binary string to StringBuilder.
44
            base2.Append(binary);
×
45

46
            // Convert remaining bytes adding leading zeros.
47
            for (idx--; idx >= 0; idx--)
×
48
            {
×
49
                base2.Append(Convert.ToString(bytes[idx], 2).PadLeft(8, '0'));
×
50
            }
×
51

52
            return base2.ToString();
×
53
        }
×
54

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

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

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

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

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

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

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

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

111
            // Append first converted chunk to StringBuilder.
112
            base8.Append(octal);
×
113

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

121
            return base8.ToString();
×
122
        }
×
123

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

156
            // if the byte array is same size as output, we'll perform the operations in place
157
            byte[] output = bytes.Length != length ? new byte[length] : bytes;
×
158

159
            // invert all the source bytes
160
            for (int i = 0; i < bytes.Length - 1; i++)
×
161
            {
×
162
                output[i] = (byte)~bytes[i];
×
163
            }
×
164

165
            // invert the remainder of the output buffer
166
            for (int i = bytes.Length - 1; i < output.Length - 1; i++)
×
167
            {
×
168
                output[i] = byte.MaxValue;
×
169
            }
×
170

171
            // ensure output value is positive and return
172
            output[output.Length - 1] = 0;
×
173
            return new BigInteger(output);
×
174
        }
×
175
}
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

© 2026 Coveralls, Inc