hexToBn function
BigInt
hexToBn(
- dynamic value, {
- Endian endian = Endian.big,
- bool isNegative = false,
})
Implementation
BigInt hexToBn(
dynamic value, {
Endian endian = Endian.big,
bool isNegative = false,
}) {
if (value == null) {
return BigInt.from(0);
}
if (isNegative == false) {
if (isHex(value)) {
String sValue = value is num
? int.parse(value.toString(), radix: 10).toRadixString(16)
: value;
sValue = hexStripPrefix(sValue);
if (endian == Endian.big) {
return BigInt.parse(sValue.isEmpty ? '0' : sValue, radix: 16);
} else {
return decodeBigInt(hexToBytes(sValue.isEmpty ? '0' : sValue));
}
}
final sValue = value is num
? int.parse(value.toString(), radix: 10).toRadixString(16)
: value;
if (endian == Endian.big) {
return BigInt.parse(sValue, radix: 16);
}
return decodeBigInt(
hexToBytes(
hexStripPrefix(sValue) == '' ? '0' : hexStripPrefix(sValue),
),
);
} else {
String hex = value is num
? int.parse(value.toString(), radix: 10).toRadixString(16)
: hexStripPrefix(value);
if (hex.length % 2 > 0) {
hex = '0$hex';
}
hex = decodeBigInt(
hexToBytes(hexStripPrefix(hex) == '' ? '0' : hexStripPrefix(hex)),
endian: endian,
).toRadixString(16);
BigInt bn = BigInt.parse(hex, radix: 16);
final result = 0x80 &
int.parse(hex.substring(0, 2 > hex.length ? hex.length : 2), radix: 16);
if (result > 0) {
BigInt some = BigInt.parse(
bn.toRadixString(2).split('').map((i) {
return '0' == i ? 1 : 0;
}).join(),
radix: 2,
);
some += BigInt.one;
bn = -some;
}
return bn;
}
}