toBytes method

Tuple<List<int>, FloatLength> toBytes(
  1. FloatLength? decodFloatType, [
  2. Endian? endianness
])

Encode the floating-point value into a byte representation using the specified floating-point format. Returns a tuple containing the encoded bytes and the format used.

Implementation

Tuple<List<int>, FloatLength> toBytes(FloatLength? decodFloatType,
    [Endian? endianness]) {
  if (decodFloatType == null) {
    if (isLessThan16) {
      return Tuple(_encodeFloat16(endianness), FloatLength.bytes16);
    } else if (isLessThan32) {
      return Tuple(_encodeFloat32(endianness), FloatLength.bytes32);
    }
    return Tuple(_encodeFloat64(endianness), FloatLength.bytes64);
  }
  final List<int> bytes;
  switch (decodFloatType) {
    case FloatLength.bytes16:
      if (!isLessThan16) {
        throw const CborException("overflow bytes");
      }
      bytes = _encodeFloat16(endianness);
      break;
    case FloatLength.bytes32:
      if (!isLessThan32) {
        throw const CborException("overflow bytes");
      }
      bytes = _encodeFloat32(endianness);
      break;
    default:
      bytes = _encodeFloat64(endianness);
      break;
  }
  return Tuple(bytes, decodFloatType);
}