diff --git a/build.vc11/mpir.net/mpir.net-tests/HugeIntTests/Conversions.cs b/build.vc11/mpir.net/mpir.net-tests/HugeIntTests/Conversions.cs index b36d5b9a..bb4e9571 100644 --- a/build.vc11/mpir.net/mpir.net-tests/HugeIntTests/Conversions.cs +++ b/build.vc11/mpir.net/mpir.net-tests/HugeIntTests/Conversions.cs @@ -93,6 +93,31 @@ namespace MPIR.Tests.HugeIntTests } } + [TestMethod] + public void FromString() + { + using (var a = new HugeInt()) + { + var n = "98762934876529834765234123984761"; + a.SetTo(n); + Assert.AreEqual(n, a.ToString()); + + n = "-98ABCDEF876529834765234123984761"; + a.SetTo(n, 16); + Assert.AreEqual(n, a.ToString(-16)); + } + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void FromInvalidString() + { + using (var a = new HugeInt()) + { + a.SetTo("12345A"); + } + } + //todo truncated test } } diff --git a/build.vc11/mpir.net/mpir.net/HugeInt.cpp b/build.vc11/mpir.net/mpir.net/HugeInt.cpp index a83bedc7..5c99568d 100644 --- a/build.vc11/mpir.net/mpir.net/HugeInt.cpp +++ b/build.vc11/mpir.net/mpir.net/HugeInt.cpp @@ -104,10 +104,20 @@ namespace MPIR if(!success) { DeallocateStruct(); - throw gcnew ArgumentException("InvalidNumber", "value"); + throw gcnew ArgumentException("Invalid number", "value"); } } + void HugeInt::SetTo(String^ value, int base) + { + IntPtr ptr = Marshal::StringToHGlobalAnsi(value); + bool success = 0 == mpz_set_str(_value, (char*)(void*)ptr, base); + Marshal::FreeHGlobal(ptr); + + if(!success) + throw gcnew ArgumentException("Invalid number", "value"); + } + HugeInt^ HugeInt::FromLong(mpir_si value) { auto result = gcnew HugeInt(); diff --git a/build.vc11/mpir.net/mpir.net/HugeInt.h b/build.vc11/mpir.net/mpir.net/HugeInt.h index 1d1ae9c2..0edea396 100644 --- a/build.vc11/mpir.net/mpir.net/HugeInt.h +++ b/build.vc11/mpir.net/mpir.net/HugeInt.h @@ -359,10 +359,13 @@ namespace MPIR //conversions mpir_ui ToUlong() { return mpz_get_ui(_value); } - void SetTo(mpir_ui value) { mpz_set_ui(_value, value); } mpir_si ToLong() { return mpz_get_si(_value); } - void SetTo(mpir_si value) { mpz_set_si(_value, value); } double ToDouble() { return mpz_get_d(_value); } + + void SetTo(mpir_ui value) { mpz_set_ui(_value, value); } + void SetTo(mpir_si value) { mpz_set_si(_value, value); } void SetTo(double value) { mpz_set_d(_value, value); } + void SetTo(String^ value) { SetTo(value, 10); } + void SetTo(String^ value, int base); }; }; \ No newline at end of file