addrToBytes static method

ByteArray addrToBytes(
  1. ByteBuffer addr
)

Convert address buffer to bytes in little-endian format. This function reads bytes from the ByteBuffer and manually reorders them to produce a little-endian byte array, assuming the ByteBuffer's get_() method reads bytes in the order they were put (likely big-endian if putInt was used with BIG_ENDIAN order, or just sequential).

@param addr The ByteBuffer containing the address data. @returns A ByteArray (Uint8List) representing the address in little-endian format.

Implementation

static ByteArray addrToBytes(ByteBuffer addr) {
  addr.position(0); // Reset position to the beginning of the buffer
  final littleEndians = Uint8List(
      addr.capacity()); // Create a new Uint8List of the same capacity

  // Loop through the buffer, reading 4 bytes at a time and reordering them
  // from big-endian (as read by get_()) to little-endian in the output array.
  for (int i = 0; i < littleEndians.length; i += 4) {
    final int b0 =
        addr.get_(); // Read byte 0 (most significant if big-endian)
    final int b1 = addr.get_(); // Read byte 1
    final int b2 = addr.get_(); // Read byte 2
    final int b3 =
        addr.get_(); // Read byte 3 (least significant if big-endian)

    // Place them in little-endian order in the output array
    littleEndians[i] = b3;
    littleEndians[i + 1] = b2;
    littleEndians[i + 2] = b1;
    littleEndians[i + 3] = b0;
  }

  return littleEndians;
}