Reputation: 533
I'm actually working on a IBAN key verification function.
To get the key i do something like :
string theKey = (98 - ((int64.Parse(value)) % 97)).ToString();
The problem is that my value is something longer than 19. So i need to use BigInteger from System.Numerics.
This references doesn't include the Parse() method.
I need a solution that would allow me to use 23char integer on Silverlight.
Upvotes: 0
Views: 78
Reputation: 533
Someone on MSDN gave me a class that comes with Parse/TryParse, it works really good and i hope it'll help. Thanks for the decimal solution though, but it appears that i need to use 30 digits int as well, so biginteger was a must have :
public static class BigIntegerHelper
{
public static BigInteger Parse(string s)
{
return Parse(s, CultureInfo.CurrentCulture);
}
public static BigInteger Parse(string s, IFormatProvider provider)
{
return Parse(s, NumberStyles.Integer, provider);
}
public static BigInteger Parse(string s, NumberStyles style)
{
return Parse(s, style, CultureInfo.CurrentCulture);
}
public static BigInteger Parse(string s, NumberStyles style, IFormatProvider provider)
{
BigInteger result;
if (TryParse(s, style, provider, out result))
{
return result;
}
throw new FormatException();
}
public static bool TryParse(string s, out BigInteger b)
{
return TryParse(s, NumberStyles.Integer, CultureInfo.CurrentCulture, out b);
}
public static bool TryParse(string s, NumberStyles style, IFormatProvider formatProvider, out BigInteger value)
{
if (formatProvider == null)
{
formatProvider = CultureInfo.CurrentCulture;
}
if ((style & ~(NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingSign | NumberStyles.AllowHexSpecifier)) != NumberStyles.None)
{
throw new NotSupportedException();
}
NumberFormatInfo numberFormatInfo = (NumberFormatInfo)formatProvider.GetFormat(typeof(NumberFormatInfo));
uint num = ((style & NumberStyles.AllowHexSpecifier) != NumberStyles.None) ? 16u : 10u;
int num2 = 0;
bool flag = false;
if ((style & NumberStyles.AllowLeadingWhite) != NumberStyles.None)
{
while (num2 < s.Length && IsWhiteSpace(s[num2]))
{
num2++;
}
}
if ((style & NumberStyles.AllowLeadingSign) != NumberStyles.None)
{
int length = numberFormatInfo.NegativeSign.Length;
if (length + num2 < s.Length && string.Compare(s, num2, numberFormatInfo.NegativeSign, 0, length, CultureInfo.CurrentCulture, CompareOptions.None) == 0)
{
flag = true;
num2 += numberFormatInfo.NegativeSign.Length;
}
}
value = BigInteger.Zero;
BigInteger bigInteger = BigInteger.One;
if (num2 == s.Length)
{
return false;
}
for (int i = s.Length - 1; i >= num2; i--)
{
if ((style & NumberStyles.AllowTrailingWhite) != NumberStyles.None && IsWhiteSpace(s[i]))
{
int num3 = i;
while (num3 >= num2 && IsWhiteSpace(s[num3]))
{
num3--;
}
if (num3 < num2)
{
return false;
}
i = num3;
}
uint num4;
if (!ParseSingleDigit(s[i], (ulong)num, out num4))
{
return false;
}
if (num4 != 0u)
{
value += num4 * bigInteger;
}
bigInteger *= num;
}
if (value.Sign == 1 && flag)
{
value = -value;
}
return true;
}
private static bool IsWhiteSpace(char ch)
{
return ch == ' ' || (ch >= '\t' && ch <= '\r');
}
private static bool ParseSingleDigit(char c, ulong radix, out uint result)
{
result = 0;
if (c >= '0' && c <= '9')
{
result = (uint)(c - '0');
return true;
}
if (radix == 16uL)
{
c = (char)((int)c & -33);
if (c >= 'A' && c <= 'F')
{
result = (uint)(c - 'A' + '\n');
return true;
}
}
return false;
}
}
Upvotes: 0
Reputation: 2852
Yep i dont think BigInteger.Parse()
is available in silverlight.
You could use Decimal
just without a decimal point, as a decimal value can go up to 79,228,162,514,264,337,593,543,950,335.
(29 chars) if i counted correctly..
*Edit - the reason i chose decimal
over double
is that decimal
has more significant figures and can therefore be more precise.
Upvotes: 1