< Summary - Combined Code Coverage

Information
Class: NLightning.Infrastructure.Converters.EndianBitConverter
Assembly: NLightning.Infrastructure
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure/Converters/EndianBitConverter.cs
Tag: 30_15166811759
Line coverage
85%
Covered lines: 126
Uncovered lines: 21
Coverable lines: 147
Total lines: 516
Line coverage: 85.7%
Branch coverage
88%
Covered branches: 111
Total branches: 126
Branch coverage: 88%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
GetBytesBigEndian(...)100%66100%
GetBytesBigEndian(...)100%66100%
GetBytesBigEndian(...)100%66100%
GetBytesBigEndian(...)100%66100%
GetBytesBigEndian(...)100%66100%
GetBytesBigEndian(...)100%66100%
GetBytesLittleEndian(...)83.33%6.11685.71%
GetBytesLittleEndian(...)83.33%6.11685.71%
GetBytesLittleEndian(...)83.33%6.11685.71%
ToUInt64LittleEndian(...)66.67%8.83657.14%
ToInt64LittleEndian(...)66.67%8.83657.14%
ToUInt32LittleEndian(...)66.67%8.83657.14%
ToInt32LittleEndian(...)66.67%8.83657.14%
ToUInt16LittleEndian(...)66.67%8.83657.14%
ToInt16LittleEndian(...)66.67%8.83657.14%
ToUInt64BigEndian(...)100%66100%
ToInt64BigEndian(...)100%66100%
ToUInt32BigEndian(...)100%66100%
ToInt32BigEndian(...)100%66100%
ToUInt16BigEndian(...)100%66100%
ToInt16BigEndian(...)100%66100%

File(s)

/home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure/Converters/EndianBitConverter.cs

#LineLine coverage
 1namespace NLightning.Infrastructure.Converters;
 2
 3public static class EndianBitConverter
 4{
 5    #region GetBytesBE
 6    /// <summary>
 7    /// Converts a ulong to a byte array in big-endian order.
 8    /// </summary>
 9    /// <param name="value">The ulong to convert.</param>
 10    /// <param name="trimToMinimumLenght">If true, the byte array will be trimmed to the minimum length.</param>
 11    /// <returns>The byte array representation of the ulong.</returns>
 12    /// <remarks>Trimming to minimum length is useful when the byte array is used in a context where the length is known
 13    public static byte[] GetBytesBigEndian(ulong value, bool trimToMinimumLenght = false)
 14    {
 13615        var bytes = BitConverter.GetBytes(value);
 13616        if (BitConverter.IsLittleEndian)
 17        {
 13618            Array.Reverse(bytes);
 19        }
 20
 13621        if (!trimToMinimumLenght)
 22        {
 12823            return bytes;
 24        }
 25
 6826        var firstNonZeroIndex = Array.FindIndex(bytes, b => b != 0);
 827        return firstNonZeroIndex == -1 ? [0] : bytes[firstNonZeroIndex..];
 28    }
 29
 30    /// <summary>
 31    /// Converts a long to a byte array in big-endian order.
 32    /// </summary>
 33    /// <param name="value">The long to convert.</param>
 34    /// <param name="trimToMinimumLenght">If true, the byte array will be trimmed to the minimum length.</param>
 35    /// <returns>The byte array representation of the long.</returns>
 36    /// <remarks>Trimming to minimum length is useful when the byte array is used in a context where the length is known
 37    public static byte[] GetBytesBigEndian(long value, bool trimToMinimumLenght = false)
 38    {
 8039        var bytes = BitConverter.GetBytes(value);
 8040        if (BitConverter.IsLittleEndian)
 41        {
 8042            Array.Reverse(bytes);
 43        }
 44
 8045        if (!trimToMinimumLenght)
 46        {
 7247            return bytes;
 48        }
 49
 6850        var firstNonZeroIndex = Array.FindIndex(bytes, b => b != 0);
 851        return firstNonZeroIndex == -1 ? [0] : bytes[firstNonZeroIndex..];
 52    }
 53
 54    /// <summary>
 55    /// Converts a uint to a byte array in big-endian order.
 56    /// </summary>
 57    /// <param name="value">The uint to convert.</param>
 58    /// <param name="trimToMinimumLenght">If true, the byte array will be trimmed to the minimum length.</param>
 59    /// <returns>The byte array representation of the uint.</returns>
 60    /// <remarks>Trimming to minimum length is useful when the byte array is used in a context where the length is known
 61    public static byte[] GetBytesBigEndian(uint value, bool trimToMinimumLenght = false)
 62    {
 9263        var bytes = BitConverter.GetBytes(value);
 9264        if (BitConverter.IsLittleEndian)
 65        {
 9266            Array.Reverse(bytes);
 67        }
 68
 9269        if (!trimToMinimumLenght)
 70        {
 8471            return bytes;
 72        }
 73
 3674        var firstNonZeroIndex = Array.FindIndex(bytes, b => b != 0);
 875        return firstNonZeroIndex == -1 ? [0] : bytes[firstNonZeroIndex..];
 76    }
 77
 78    /// <summary>
 79    /// Converts a int to a byte array in big-endian order.
 80    /// </summary>
 81    /// <param name="value">The int to convert.</param>
 82    /// <param name="trimToMinimumLenght">If true, the byte array will be trimmed to the minimum length.</param>
 83    /// <returns>The byte array representation of the int.</returns>
 84    /// <remarks>Trimming to minimum length is useful when the byte array is used in a context where the length is known
 85    public static byte[] GetBytesBigEndian(int value, bool trimToMinimumLenght = false)
 86    {
 1287        var bytes = BitConverter.GetBytes(value);
 1288        if (BitConverter.IsLittleEndian)
 89        {
 1290            Array.Reverse(bytes);
 91        }
 92
 1293        if (!trimToMinimumLenght)
 94        {
 495            return bytes;
 96        }
 97
 3698        var firstNonZeroIndex = Array.FindIndex(bytes, b => b != 0);
 899        return firstNonZeroIndex == -1 ? [0] : bytes[firstNonZeroIndex..];
 100    }
 101
 102    /// <summary>
 103    /// Converts a ushort to a byte array in big-endian order.
 104    /// </summary>
 105    /// <param name="value">The ushort to convert.</param>
 106    /// <param name="trimToMinimumLenght">If true, the byte array will be trimmed to the minimum length.</param>
 107    /// <returns>The byte array representation of the ushort.</returns>
 108    /// <remarks>Trimming to minimum length is useful when the byte array is used in a context where the length is known
 109    public static byte[] GetBytesBigEndian(ushort value, bool trimToMinimumLenght = false)
 110    {
 252111        var bytes = BitConverter.GetBytes(value);
 252112        if (BitConverter.IsLittleEndian)
 113        {
 252114            Array.Reverse(bytes);
 115        }
 116
 252117        if (!trimToMinimumLenght)
 118        {
 244119            return bytes;
 120        }
 121
 24122        var firstNonZeroIndex = Array.FindIndex(bytes, b => b != 0);
 8123        return firstNonZeroIndex == -1 ? [0] : bytes[firstNonZeroIndex..];
 124    }
 125
 126    /// <summary>
 127    /// Converts a short to a byte array in big-endian order.
 128    /// </summary>
 129    /// <param name="value">The short to convert.</param>
 130    /// <param name="trimToMinimumLenght">If true, the byte array will be trimmed to the minimum length.</param>
 131    /// <returns>The byte array representation of the short.</returns>
 132    /// <remarks>Trimming to minimum length is useful when the byte array is used in a context where the length is known
 133    public static byte[] GetBytesBigEndian(short value, bool trimToMinimumLenght = false)
 134    {
 12135        var bytes = BitConverter.GetBytes(value);
 12136        if (BitConverter.IsLittleEndian)
 137        {
 12138            Array.Reverse(bytes);
 139        }
 140
 12141        if (!trimToMinimumLenght)
 142        {
 4143            return bytes;
 144        }
 145
 24146        var firstNonZeroIndex = Array.FindIndex(bytes, b => b != 0);
 8147        return firstNonZeroIndex == -1 ? [0] : bytes[firstNonZeroIndex..];
 148    }
 149    #endregion
 150
 151    #region GetBytesLE
 152    /// <summary>
 153    /// Converts a ulong to a byte array in little-endian order.
 154    /// </summary>
 155    /// <param name="value">The ulong to convert.</param>
 156    /// <param name="trimToMinimumLenght">If true, the byte array will be trimmed to the minimum length.</param>
 157    /// <returns>The byte array representation of the ulong.</returns>
 158    /// <remarks>Trimming to minimum length is useful when the byte array is used in a context where the length is known
 159    public static byte[] GetBytesLittleEndian(ulong value, bool trimToMinimumLenght = false)
 160    {
 24161        var bytes = BitConverter.GetBytes(value);
 24162        if (!BitConverter.IsLittleEndian)
 163        {
 0164            Array.Reverse(bytes);
 165        }
 166
 24167        if (!trimToMinimumLenght)
 168        {
 8169            return bytes;
 170        }
 171
 88172        var firstNonZeroIndex = Array.FindIndex(bytes, b => b != 0);
 16173        return firstNonZeroIndex == -1 ? [0] : bytes[firstNonZeroIndex..];
 174    }
 175
 176    /// <summary>
 177    /// Converts a uint to a byte array in little-endian order.
 178    /// </summary>
 179    /// <param name="value">The uint to convert.</param>
 180    /// <param name="trimToMinimumLenght">If true, the byte array will be trimmed to the minimum length.</param>
 181    /// <returns>The byte array representation of the uint.</returns>
 182    /// <remarks>Trimming to minimum length is useful when the byte array is used in a context where the length is known
 183    public static byte[] GetBytesLittleEndian(uint value, bool trimToMinimumLenght = false)
 184    {
 12185        var bytes = BitConverter.GetBytes(value);
 12186        if (!BitConverter.IsLittleEndian)
 187        {
 0188            Array.Reverse(bytes);
 189        }
 190
 12191        if (!trimToMinimumLenght)
 192        {
 4193            return bytes;
 194        }
 195
 28196        var firstNonZeroIndex = Array.FindIndex(bytes, b => b != 0);
 8197        return firstNonZeroIndex == -1 ? [0] : bytes[firstNonZeroIndex..];
 198    }
 199
 200    /// <summary>
 201    /// Converts a ushort to a byte array in little-endian order.
 202    /// </summary>
 203    /// <param name="value">The ushort to convert.</param>
 204    /// <param name="trimToMinimumLenght">If true, the byte array will be trimmed to the minimum length.</param>
 205    /// <returns>The byte array representation of the ushort.</returns>
 206    /// <remarks>Trimming to minimum length is useful when the byte array is used in a context where the length is known
 207    public static byte[] GetBytesLittleEndian(ushort value, bool trimToMinimumLenght = false)
 208    {
 24209        var bytes = BitConverter.GetBytes(value);
 24210        if (!BitConverter.IsLittleEndian)
 211        {
 0212            Array.Reverse(bytes);
 213        }
 214
 24215        if (!trimToMinimumLenght)
 216        {
 8217            return bytes;
 218        }
 219
 40220        var firstNonZeroIndex = Array.FindIndex(bytes, b => b != 0);
 16221        return firstNonZeroIndex == -1 ? [0] : bytes[firstNonZeroIndex..];
 222    }
 223    #endregion
 224
 225    #region Back From LE Bytes
 226    /// <summary>
 227    /// Converts a byte array to an ulong in little-endian order.
 228    /// </summary>
 229    /// <param name="bytes">The byte array to convert.</param>
 230    /// <param name="padWithZero">If true, the byte array will be padded with zero if the length is less than 8.</param>
 231    /// <returns>The ulong representation of the byte array.</returns>
 232    /// <remarks>Padding with zero is useful when the byte array is used in a context where the length is known to be le
 233    public static ulong ToUInt64LittleEndian(ReadOnlySpan<byte> bytes, bool padWithZero = false)
 234    {
 235        // pad with zero if the length is less than 8
 16236        var paddedBytes = bytes.ToArray();
 16237        if (padWithZero && bytes.Length < 8)
 238        {
 0239            paddedBytes = new byte[8];
 0240            bytes.CopyTo(paddedBytes.AsSpan()[(8 - bytes.Length)..]);
 241        }
 242
 16243        if (!BitConverter.IsLittleEndian)
 244        {
 0245            Array.Reverse(paddedBytes);
 246        }
 16247        return BitConverter.ToUInt64(paddedBytes, 0);
 248    }
 249
 250    /// <summary>
 251    /// Converts a byte array to a ulong in little-endian order.
 252    /// </summary>
 253    /// <param name="bytes">The byte array to convert.</param>
 254    /// <param name="padWithZero">If true, the byte array will be padded with zero if the length is less than 8.</param>
 255    /// <returns>The ulong representation of the byte array.</returns>
 256    /// <remarks>Padding with zero is useful when the byte array is used in a context where the length is known to be le
 257    public static long ToInt64LittleEndian(ReadOnlySpan<byte> bytes, bool padWithZero = false)
 258    {
 259        // pad with zero if the length is less than 8
 16260        var paddedBytes = bytes.ToArray();
 16261        if (padWithZero && bytes.Length < 8)
 262        {
 0263            paddedBytes = new byte[8];
 0264            bytes.CopyTo(paddedBytes.AsSpan()[(8 - bytes.Length)..]);
 265        }
 266
 16267        if (!BitConverter.IsLittleEndian)
 268        {
 0269            Array.Reverse(paddedBytes);
 270        }
 16271        return BitConverter.ToInt64(paddedBytes, 0);
 272    }
 273
 274    /// <summary>
 275    /// Converts a byte array to a uint in little-endian order.
 276    /// </summary>
 277    /// <param name="bytes">The byte array to convert.</param>
 278    /// <param name="padWithZero">If true, the byte array will be padded with zero if the length is less than 4.</param>
 279    /// <returns>The uint representation of the byte array.</returns>
 280    /// <remarks>Padding with zero is useful when the byte array is used in a context where the length is known to be le
 281    public static uint ToUInt32LittleEndian(ReadOnlySpan<byte> bytes, bool padWithZero = false)
 282    {
 283        // pad with zero if the length is less than 4
 16284        var paddedBytes = bytes.ToArray();
 16285        if (padWithZero && bytes.Length < 4)
 286        {
 0287            paddedBytes = new byte[4];
 0288            bytes.CopyTo(paddedBytes.AsSpan()[(4 - bytes.Length)..]);
 289        }
 290
 16291        if (!BitConverter.IsLittleEndian)
 292        {
 0293            Array.Reverse(paddedBytes);
 294        }
 16295        return BitConverter.ToUInt32(paddedBytes, 0);
 296    }
 297
 298    /// <summary>
 299    /// Converts a byte array to a uint in little-endian order.
 300    /// </summary>
 301    /// <param name="bytes">The byte array to convert.</param>
 302    /// <param name="padWithZero">If true, the byte array will be padded with zero if the length is less than 4.</param>
 303    /// <returns>The uint representation of the byte array.</returns>
 304    /// <remarks>Padding with zero is useful when the byte array is used in a context where the length is known to be le
 305    public static int ToInt32LittleEndian(ReadOnlySpan<byte> bytes, bool padWithZero = false)
 306    {
 307        // pad with zero if the length is less than 4
 16308        var paddedBytes = bytes.ToArray();
 16309        if (padWithZero && bytes.Length < 4)
 310        {
 0311            paddedBytes = new byte[4];
 0312            bytes.CopyTo(paddedBytes.AsSpan()[(4 - bytes.Length)..]);
 313        }
 314
 16315        if (!BitConverter.IsLittleEndian)
 316        {
 0317            Array.Reverse(paddedBytes);
 318        }
 16319        return BitConverter.ToInt32(paddedBytes, 0);
 320    }
 321
 322    /// <summary>
 323    /// Converts a byte array to a ushort in little-endian order.
 324    /// </summary>
 325    /// <param name="bytes">The byte array to convert.</param>
 326    /// <param name="padWithZero">If true, the byte array will be padded with zero if the length is less than 2.</param>
 327    /// <returns>The ushort representation of the byte array.</returns>
 328    /// <remarks>Padding with zero is useful when the byte array is used in a context where the length is known to be le
 329    public static ushort ToUInt16LittleEndian(ReadOnlySpan<byte> bytes, bool padWithZero = false)
 330    {
 331        // pad with zero if the length is less than 2
 16332        var paddedBytes = bytes.ToArray();
 16333        if (padWithZero && bytes.Length < 2)
 334        {
 0335            paddedBytes = new byte[2];
 0336            bytes.CopyTo(paddedBytes.AsSpan()[(2 - bytes.Length)..]);
 337        }
 338
 16339        if (!BitConverter.IsLittleEndian)
 340        {
 0341            Array.Reverse(paddedBytes);
 342        }
 16343        return BitConverter.ToUInt16(paddedBytes, 0);
 344    }
 345
 346    /// <summary>
 347    /// Converts a byte array to a ushort in little-endian order.
 348    /// </summary>
 349    /// <param name="bytes">The byte array to convert.</param>
 350    /// <param name="padWithZero">If true, the byte array will be padded with zero if the length is less than 2.</param>
 351    /// <returns>The ushort representation of the byte array.</returns>
 352    /// <remarks>Padding with zero is useful when the byte array is used in a context where the length is known to be le
 353    public static short ToInt16LittleEndian(ReadOnlySpan<byte> bytes, bool padWithZero = false)
 354    {
 355        // pad with zero if the length is less than 2
 16356        var paddedBytes = bytes.ToArray();
 16357        if (padWithZero && bytes.Length < 2)
 358        {
 0359            paddedBytes = new byte[2];
 0360            bytes.CopyTo(paddedBytes.AsSpan()[(2 - bytes.Length)..]);
 361        }
 362
 16363        if (!BitConverter.IsLittleEndian)
 364        {
 0365            Array.Reverse(paddedBytes);
 366        }
 16367        return BitConverter.ToInt16(paddedBytes, 0);
 368    }
 369    #endregion
 370
 371    #region Back From BE Bytes
 372    /// <summary>
 373    /// Converts a byte array to a ulong in big-endian order.
 374    /// </summary>
 375    /// <param name="bytes">The byte array to convert.</param>
 376    /// <param name="padWithZero">If true, the byte array will be padded with zero if the length is less than 8.</param>
 377    /// <returns>The ulong representation of the byte array.</returns>
 378    /// <remarks>Padding with zero is useful when the byte array is used in a context where the length is known to be le
 379    public static ulong ToUInt64BigEndian(ReadOnlySpan<byte> bytes, bool padWithZero = false)
 380    {
 381        // pad with zero if the length is less than 8
 232382        var paddedBytes = bytes.ToArray();
 232383        if (padWithZero && bytes.Length < 8)
 384        {
 4385            paddedBytes = new byte[8];
 4386            bytes.CopyTo(paddedBytes.AsSpan()[(8 - bytes.Length)..]);
 387        }
 388
 232389        if (BitConverter.IsLittleEndian)
 390        {
 232391            Array.Reverse(paddedBytes);
 392        }
 232393        return BitConverter.ToUInt64(paddedBytes, 0);
 394    }
 395
 396    /// <summary>
 397    /// Converts a byte array to a ulong in big-endian order.
 398    /// </summary>
 399    /// <param name="bytes">The byte array to convert.</param>
 400    /// <param name="padWithZero">If true, the byte array will be padded with zero if the length is less than 8.</param>
 401    /// <returns>The ulong representation of the byte array.</returns>
 402    /// <remarks>Padding with zero is useful when the byte array is used in a context where the length is known to be le
 403    public static long ToInt64BigEndian(byte[] bytes, bool padWithZero = false)
 404    {
 405        // pad with zero if the length is less than 8
 28406        var paddedBytes = bytes.ToArray();
 28407        if (padWithZero && bytes.Length < 8)
 408        {
 4409            paddedBytes = new byte[8];
 4410            bytes.CopyTo(paddedBytes.AsSpan()[(8 - bytes.Length)..]);
 411        }
 412
 28413        if (BitConverter.IsLittleEndian)
 414        {
 28415            Array.Reverse(paddedBytes);
 416        }
 28417        return BitConverter.ToInt64(paddedBytes, 0);
 418    }
 419
 420    /// <summary>
 421    /// Converts a byte array to a uint in big-endian order.
 422    /// </summary>
 423    /// <param name="bytes">The byte array to convert.</param>
 424    /// <param name="padWithZero">If true, the byte array will be padded with zero if the length is less than 4.</param>
 425    /// <returns>The uint representation of the byte array.</returns>
 426    /// <remarks>Padding with zero is useful when the byte array is used in a context where the length is known to be le
 427    public static uint ToUInt32BigEndian(ReadOnlySpan<byte> bytes, bool padWithZero = false)
 428    {
 429        // pad with zero if the length is less than 4
 124430        var paddedBytes = bytes.ToArray();
 124431        if (padWithZero && bytes.Length < 4)
 432        {
 4433            paddedBytes = new byte[4];
 4434            bytes.CopyTo(paddedBytes.AsSpan()[(4 - bytes.Length)..]);
 435        }
 436
 124437        if (BitConverter.IsLittleEndian)
 438        {
 124439            Array.Reverse(paddedBytes);
 440        }
 124441        return BitConverter.ToUInt32(paddedBytes, 0);
 442    }
 443
 444    /// <summary>
 445    /// Converts a byte array to a uint in big-endian order.
 446    /// </summary>
 447    /// <param name="bytes">The byte array to convert.</param>
 448    /// <param name="padWithZero">If true, the byte array will be padded with zero if the length is less than 4.</param>
 449    /// <returns>The uint representation of the byte array.</returns>
 450    /// <remarks>Padding with zero is useful when the byte array is used in a context where the length is known to be le
 451    public static int ToInt32BigEndian(ReadOnlySpan<byte> bytes, bool padWithZero = false)
 452    {
 453        // pad with zero if the length is less than 4
 16454        var paddedBytes = bytes.ToArray();
 16455        if (padWithZero && bytes.Length < 4)
 456        {
 4457            paddedBytes = new byte[4];
 4458            bytes.CopyTo(paddedBytes.AsSpan()[(4 - bytes.Length)..]);
 459        }
 460
 16461        if (BitConverter.IsLittleEndian)
 462        {
 16463            Array.Reverse(paddedBytes);
 464        }
 16465        return BitConverter.ToInt32(paddedBytes, 0);
 466    }
 467
 468    /// <summary>
 469    /// Converts a byte array to a ushort in big-endian order.
 470    /// </summary>
 471    /// <param name="bytes">The byte array to convert.</param>
 472    /// <param name="padWithZero">If true, the byte array will be padded with zero if the length is less than 2.</param>
 473    /// <returns>The ushort representation of the byte array.</returns>
 474    /// <remarks>Padding with zero is useful when the byte array is used in a context where the length is known to be le
 475    public static ushort ToUInt16BigEndian(ReadOnlySpan<byte> bytes, bool padWithZero = false)
 476    {
 477        // pad with zero if the length is less than 2
 284478        var paddedBytes = bytes.ToArray();
 284479        if (padWithZero && bytes.Length < 2)
 480        {
 4481            paddedBytes = new byte[2];
 4482            bytes.CopyTo(paddedBytes.AsSpan()[(2 - bytes.Length)..]);
 483        }
 484
 284485        if (BitConverter.IsLittleEndian)
 486        {
 284487            Array.Reverse(paddedBytes);
 488        }
 284489        return BitConverter.ToUInt16(paddedBytes, 0);
 490    }
 491
 492    /// <summary>
 493    /// Converts a byte array to a ushort in big-endian order.
 494    /// </summary>
 495    /// <param name="bytes">The byte array to convert.</param>
 496    /// <param name="padWithZero">If true, the byte array will be padded with zero if the length is less than 2.</param>
 497    /// <returns>The ushort representation of the byte array.</returns>
 498    /// <remarks>Padding with zero is useful when the byte array is used in a context where the length is known to be le
 499    public static short ToInt16BigEndian(ReadOnlySpan<byte> bytes, bool padWithZero = false)
 500    {
 501        // pad with zero if the length is less than 2
 16502        var paddedBytes = bytes.ToArray();
 16503        if (padWithZero && bytes.Length < 2)
 504        {
 4505            paddedBytes = new byte[2];
 4506            bytes.CopyTo(paddedBytes.AsSpan()[(2 - bytes.Length)..]);
 507        }
 508
 16509        if (BitConverter.IsLittleEndian)
 510        {
 16511            Array.Reverse(paddedBytes);
 512        }
 16513        return BitConverter.ToInt16(paddedBytes, 0);
 514    }
 515    #endregion
 516}