diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Common/Extensions.cs | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/slskd/Common/Extensions.cs b/src/slskd/Common/Extensions.cs index 32a9b26e..d678541d 100644 --- a/src/slskd/Common/Extensions.cs +++ b/src/slskd/Common/Extensions.cs @@ -22,7 +22,9 @@ namespace slskd using System.IO; using System.Linq; using System.Net.Sockets; + using System.Numerics; using System.Reflection; + using System.Text; using System.Text.Json; using System.Text.Json.Serialization; using System.Text.RegularExpressions; @@ -285,6 +287,27 @@ namespace slskd } /// <summary> + /// Converts the byte array into a base 62 encoded string. + /// </summary> + /// <param name="bytes">The bytes to convert.</param> + /// <returns>The converted bytes as a base 62 string.</returns> + public static string ToBase62String(this byte[] bytes) + { + const string alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + BigInteger dividend = new BigInteger(bytes); + var builder = new StringBuilder(); + + while (dividend != 0) + { + dividend = BigInteger.DivRem(dividend, alphabet.Length, out BigInteger remainder); + builder.Insert(0, alphabet[Math.Abs((int)remainder)]); + } + + return builder.ToString(); + } + + /// <summary> /// Creates a copy of this instance with the specified parameters changed. /// </summary> /// <param name="o">The options instance to copy.</param> |
