BigInteger Parse Octal String?

后端 未结 2 570
无人及你
无人及你 2020-12-06 13:06

In Java, I could do

//Parsing Octal String
BigInteger b = new BigInteger(\"16304103460644701340432043410021040424210140423204\",8);

Then f

2条回答
  •  爱一瞬间的悲伤
    2020-12-06 13:26

    This may not be the most efficient solution, but if performance is not a priority, you can construct the BigInteger manually:

    string s = "16304103460644701340432043410021040424210140423204";
    BigInteger bi = s.Aggregate(new BigInteger(), (b, c) => b * 8 + c - '0');
    

    The above solution also works for any base not greater than 10; just replace the 8 in the above code with your required base.

    Edit: For hexadecimal numbers, you should use the Parse method. Prepend with 0 if your number should be interpreted as positive even if its first character is 8F.

    string s = "0F20051C5E45F4FD68F8E58905A133BCA";
    BigInteger bi = BigInteger.Parse(s, NumberStyles.HexNumber);
    

提交回复
热议问题