<?php
echo "Running script version: 2025-03-08-14\n";

$file = fopen('1.DAT', 'rb');

$headerLength = 288;
$header = fread($file, $headerLength);
echo "Header hex: " . bin2hex($header) . "\n";
echo "Header length: " . strlen($header) . "\n";

fseek($file, $headerLength);

$recordNum = 1;
while (!feof($file) && $recordNum <= 5) {
    $record = fread($file, 39);
    if (strlen($record) != 39) {
        echo "Record $recordNum too short: " . strlen($record) . " bytes\n";
        break;
    }

    echo "Registro Nro: $recordNum\n";
    echo "Record hex (raw): " . bin2hex($record) . "\n";

    $timestamp = substr($record, 0, 19);
    echo "Timestamp: '$timestamp'\n";

    $padding = substr($record, 23, 5); // Was 19, now 23
    echo "Padding: '" . bin2hex($padding) . "'\n";

    $tagIndex = ord(substr($record, 28, 1)); // Was 24, now 28
    echo "TagIndex: $tagIndex\n";

    $valueBin = substr($record, 29, 8); // Was 25, now 29
    $valueBig = unpack('d', strrev($valueBin))[1];
    echo "Value hex: " . bin2hex($valueBin) . "\n";
    echo "Value (big-endian): $valueBig\n";

    $status = ord(substr($record, 37, 1)); // Was 33, now 37
    echo "Status: $status\n";

    $marker = substr($record, 38, 1); // Was 34, now 38
    echo "Marker: '$marker'\n";

    $internalBin = substr($record, 39, 4); // Was 35, now 39 (but 39+4=43 exceeds length)
    // Adjust to read only available bytes
    $internalBin = substr($record, 35, 4); // Keep at 35 since record is 39 bytes
    $internal = unpack('l', $internalBin)[1];
    echo "Internal hex: " . bin2hex($internalBin) . "\n";
    echo "Internal: $internal\n";

    echo "\n";
    $recordNum++;
}
fclose($file);
