mirror of
https://github.com/nsnail/ns-ext.git
synced 2025-07-01 23:48:16 +08:00
feat: ✨ 转36进制 (#38)
[skip ci] Co-authored-by: tk <fiyne1a@dingtalk.com>
This commit is contained in:
@ -5,6 +5,8 @@ namespace NSExt.Extensions;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static class LongExtensions
|
public static class LongExtensions
|
||||||
{
|
{
|
||||||
|
private const string _CHARS_36 = "0123456789abcdefghijklmnopqrstuvwxyz";
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 判断枚举是否包含某个位
|
/// 判断枚举是否包含某个位
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -24,6 +26,22 @@ public static class LongExtensions
|
|||||||
return new Random(Guid.NewGuid().GetHashCode()).NextInt64(me[0], me[1]);
|
return new Random(Guid.NewGuid().GetHashCode()).NextInt64(me[0], me[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 转36进制
|
||||||
|
/// </summary>
|
||||||
|
public static string Sys36(this long me)
|
||||||
|
{
|
||||||
|
var ret = new StringBuilder();
|
||||||
|
while (me > 35)
|
||||||
|
{
|
||||||
|
_ = ret.Insert(0, _CHARS_36[(int)(me % 36)]);
|
||||||
|
me /= 36;
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = ret.Insert(0, _CHARS_36[(int)me]);
|
||||||
|
return ret.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 1970毫秒数转换成日期对象
|
/// 1970毫秒数转换成日期对象
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -14,7 +14,7 @@ namespace NSExt.Extensions;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static partial class StringExtensions
|
public static partial class StringExtensions
|
||||||
{
|
{
|
||||||
private const string _CHARACTERS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
private const string _CHARS_62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||||
private static readonly Regex _regexIpV4 = RegexIpV4();
|
private static readonly Regex _regexIpV4 = RegexIpV4();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -51,15 +51,43 @@ public static partial class StringExtensions
|
|||||||
return decrypted.HexDe();
|
return decrypted.HexDe();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 将指定的输入字符串进行Base62编码
|
||||||
|
/// </summary>
|
||||||
|
public static string Base62(this string me)
|
||||||
|
{
|
||||||
|
// Convert string to byte array
|
||||||
|
var bytes = Encoding.UTF8.GetBytes(me);
|
||||||
|
|
||||||
|
// Convert byte array to BigInteger for easier processing
|
||||||
|
var bigInteger = new BigInteger(bytes);
|
||||||
|
|
||||||
|
if (bigInteger == 0)
|
||||||
|
{
|
||||||
|
return _CHARS_62[0].ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = new StringBuilder();
|
||||||
|
|
||||||
|
while (bigInteger > 0)
|
||||||
|
{
|
||||||
|
var remainder = (int)(bigInteger % 62);
|
||||||
|
bigInteger /= 62;
|
||||||
|
_ = result.Insert(0, _CHARS_62[remainder]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 将指定的输入字符串进行Base62解码
|
/// 将指定的输入字符串进行Base62解码
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <exception cref="ArgumentException">ArgumentException</exception>
|
/// <exception cref="ArgumentException">ArgumentException</exception>
|
||||||
public static string Base62Decode(this string me)
|
public static string Base62De(this string me)
|
||||||
{
|
{
|
||||||
BigInteger result = 0;
|
BigInteger result = 0;
|
||||||
|
|
||||||
foreach (var index in me.Select(c => _CHARACTERS.IndexOf(c)))
|
foreach (var index in me.Select(c => _CHARS_62.IndexOf(c)))
|
||||||
{
|
{
|
||||||
if (index < 0)
|
if (index < 0)
|
||||||
{
|
{
|
||||||
@ -81,34 +109,6 @@ public static partial class StringExtensions
|
|||||||
return Encoding.UTF8.GetString(bytes);
|
return Encoding.UTF8.GetString(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 将指定的输入字符串进行Base62编码
|
|
||||||
/// </summary>
|
|
||||||
public static string Base62Encode(this string me)
|
|
||||||
{
|
|
||||||
// Convert string to byte array
|
|
||||||
var bytes = Encoding.UTF8.GetBytes(me);
|
|
||||||
|
|
||||||
// Convert byte array to BigInteger for easier processing
|
|
||||||
var bigInteger = new BigInteger(bytes);
|
|
||||||
|
|
||||||
if (bigInteger == 0)
|
|
||||||
{
|
|
||||||
return _CHARACTERS[0].ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
var result = new StringBuilder();
|
|
||||||
|
|
||||||
while (bigInteger > 0)
|
|
||||||
{
|
|
||||||
var remainder = (int)(bigInteger % 62);
|
|
||||||
bigInteger /= 62;
|
|
||||||
_ = result.Insert(0, _CHARACTERS[remainder]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result.ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// base64编码
|
/// base64编码
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
Reference in New Issue
Block a user