From 37a4ab683b6d8206a9de937285c9dbdb9e19b663 Mon Sep 17 00:00:00 2001 From: juanonsoftware Date: Tue, 28 Nov 2023 09:58:02 +0700 Subject: [PATCH] Adding ToBase36Number and ToBase62Number --- BigNumber/BigNumberExtensions.cs | 46 +++++++++++++++++++++++++++++ BigNumberTests/Base36NumberTests.cs | 14 +++++++++ BigNumberTests/Base62NumberTests.cs | 14 +++++++++ 3 files changed, 74 insertions(+) diff --git a/BigNumber/BigNumberExtensions.cs b/BigNumber/BigNumberExtensions.cs index 48a118b..863278f 100644 --- a/BigNumber/BigNumberExtensions.cs +++ b/BigNumber/BigNumberExtensions.cs @@ -1,4 +1,6 @@ using System; +using System.IO; +using System.Text; namespace BigNumber { @@ -34,6 +36,28 @@ public static Base36Number ToBase36Number(this int base10Number) return new Base36Number(BitConverter.GetBytes(base10Number)); } + /// + /// New Base36Number from base-10 number + /// + /// + /// + public static Base36Number ToBase36Number(this decimal base10Number) + { + var bytes = Encoding.ASCII.GetBytes(base10Number.ToString()); + return new Base36Number(bytes); + } + + /// + /// Get back a base-10 decimal number from Base36Number + /// + /// + /// + public static decimal AsDecimal(this Base36Number base36) + { + var str = Encoding.ASCII.GetString(base36.IntValue.ToByteArray()); + return decimal.Parse(str); + } + /// /// New Base62Number from Guid instance /// @@ -63,5 +87,27 @@ public static Base62Number ToBase62Number(this int base10Number) { return new Base62Number(BitConverter.GetBytes(base10Number)); } + + /// + /// New Base62Number from base-10 number + /// + /// + /// + public static Base62Number ToBase62Number(this decimal base10Number) + { + var bytes = Encoding.ASCII.GetBytes(base10Number.ToString()); + return new Base62Number(bytes); + } + + /// + /// Get back a base-10 decimal number from Base62Number + /// + /// + /// + public static decimal AsDecimal(this Base62Number base62) + { + var str = Encoding.ASCII.GetString(base62.IntValue.ToByteArray()); + return decimal.Parse(str); + } } } diff --git a/BigNumberTests/Base36NumberTests.cs b/BigNumberTests/Base36NumberTests.cs index 5303b66..8a62330 100644 --- a/BigNumberTests/Base36NumberTests.cs +++ b/BigNumberTests/Base36NumberTests.cs @@ -33,6 +33,20 @@ public void CanCreateNewBase36NumberFromLongNumber() Assert.IsTrue(number.IntValue > 0); } + [TestMethod] + public void CanCreateNewBase36NumberFromDecimalNumber() + { + var number = ((decimal)1000.1).ToBase36Number(); + Assert.IsTrue(number.IntValue > 0); + } + + [TestMethod] + public void CanGetDecimalFromABase36NumberOfADecimal() + { + var number = (10M).ToBase36Number(); + Assert.AreEqual(10, number.AsDecimal()); + } + [TestMethod] public void CanCreateNewBase36NumberFromString_02() { diff --git a/BigNumberTests/Base62NumberTests.cs b/BigNumberTests/Base62NumberTests.cs index 0401e15..ed6370e 100644 --- a/BigNumberTests/Base62NumberTests.cs +++ b/BigNumberTests/Base62NumberTests.cs @@ -33,6 +33,20 @@ public void CanCreateNewBase62NumberFromLongNumber() Assert.IsTrue(number.IntValue > 0); } + [TestMethod] + public void CanCreateNewBase62NumberFromDecimalNumber() + { + var number = ((decimal)1000.1).ToBase62Number(); + Assert.IsTrue(number.IntValue > 0); + } + + [TestMethod] + public void CanGetDecimalFromABase62NumberOfADecimal() + { + var number = (10M).ToBase62Number(); + Assert.AreEqual(10, number.AsDecimal()); + } + [TestMethod] public void CanCreateNewBase62NumberFromString_02() {