fromBytes static method

LOCResponseRecord fromBytes({
  1. required String name,
  2. required int ttl,
  3. required Uint8List bytes,
  4. required int offset,
  5. required int length,
})

Implementation

static LOCResponseRecord fromBytes({
    required String name,
    required int ttl,
    required Uint8List bytes,
    required int offset,
    required int length }) {

    if (length < 16 || (offset + 16) > bytes.length) {
        throw FormatException('Invalid LOC record: length too short');
    }

    final int version = bytes[offset];
    final int size = bytes[offset + 1];
    final int hPrecision = bytes[offset + 2];
    final int vPrecision = bytes[offset + 3];

    int readUint32(int pos) => (bytes[pos] << 24) |
        (bytes[pos + 1] << 16) |
        (bytes[pos + 2] << 8) |
        bytes[pos + 3];

    final int rawLat = readUint32(offset + 4);
    final int rawLon = readUint32(offset + 8);
    final int rawAlt = readUint32(offset + 12);

    // Convertir a grados decimales
    double convertCoord(int rawCoord) {
        // rawCoord = milésimas de segundos + 2^31
        return ((rawCoord - 0x80000000) / 1000.0) / 3600.0;
    }

    // Altitud en metros = (rawAlt - 100000*1000)/1000.0
    double altitude = (rawAlt - 100000000) / 1000.0;

    return LOCResponseRecord(
        name: name,
        ttl: ttl,
        version: version,
        size: size,
        horizontalPrecision: hPrecision,
        verticalPrecision: vPrecision,
        latitude: convertCoord(rawLat),
        longitude: convertCoord(rawLon),
        altitude: altitude);
}