encode method

  1. @override
String encode(
  1. BigInt bigInt
)
override

Encodes a value of type T to the storage type TStore.

Implementation

@override
String encode(BigInt bigInt) {
  // Convert to efficient binary representation
  final isNegative = bigInt.isNegative;
  final magnitude = bigInt.abs();

  // Calculate how many bytes we need
  var tempMag = magnitude;
  var byteCount = 0;
  do {
    byteCount++;
    tempMag = tempMag >> 8;
  } while (tempMag > BigInt.zero);

  // Create a byte array with an extra byte for the sign
  final bytes = Uint8List(byteCount + 1);

  // First byte indicates sign (0 for positive, 1 for negative)
  bytes[0] = isNegative ? 1 : 0;

  // Fill in the magnitude bytes in big-endian order
  var tempValue = magnitude;
  for (var i = byteCount; i > 0; i--) {
    bytes[i] = (tempValue & BigInt.from(0xFF)).toInt();
    tempValue = tempValue >> 8;
  }

  return base64Encode(bytes);
}