i have string represents large hexadecimal number want convert integer. when try convert though, answer integer equivalent negative. here's code:
string hex = "9782e78f1636"; biginteger b1 = biginteger.parse(hex, numberstyles.allowhexspecifier);
how can correct result?
you need prepend 0 string. while number works long.parse
, bigint negative number if first digit between 8-f
string hex = "09782e78f1636"; biginteger b1 = biginteger.parse(hex,numberstyles.allowhexspecifier); console.writeline(b1);
it's been long time, feel should have explained why:
signed integers use significant bit (msb) indicates whether value positive or negative. although there's more msb.
both biginteger
, long
signed integers: if significant number in hex string between 0 (0000
) , 7 (0111
), msb 0
, number positive. if it's 8 (1000
) or above, msb 1
, number negative.
long.parse
knows size of type you're parsing, ie it's hex representation 00009782e78f1636
. msb 0
it's clealy positive.
bigint
isn't of fixed size long. if give 9782e78f1636
it doesn't know mean same 00001782e78f1636
. thinks type shorter. it's expecting signed value , it thinks mean first bit of 9 (1001
) sign. adding 0 front 09782e78f1636
makes clear msl bit 0
, 1001
part of actual value.
Comments
Post a Comment