encodingMode->getModeBits(); echo "1. Mode indicator: {$modeBits} (Byte mode)\n"; // Step 2: Character count $charCountBits = $config->encodingMode->getCharacterCountBits($config->version); $dataLength = strlen($testData); $countBits = str_pad(decbin($dataLength), $charCountBits, '0', STR_PAD_LEFT); echo "2. Character count ({$charCountBits} bits): {$countBits} = {$dataLength}\n"; // Step 3: Data bytes $dataBitsStr = ''; for ($i = 0; $i < $dataLength; $i++) { $byte = ord($testData[$i]); $byteBits = str_pad(decbin($byte), 8, '0', STR_PAD_LEFT); $dataBitsStr .= $byteBits; echo "3.{$i} Data byte '{$testData[$i]}': {$byteBits} = {$byte}\n"; } echo "\n"; // Step 4: Build bit string $bits = $modeBits . $countBits . $dataBitsStr; echo "Total bits before terminator: " . strlen($bits) . "\n"; // Step 5: Terminator $ecInfo = ReedSolomonEncoder::getECInfo( $config->version->getVersionNumber(), $config->errorCorrectionLevel->value ); $requiredBits = $ecInfo['dataCodewords'] * 8; echo "Required bits: {$requiredBits}\n"; $terminatorLength = min(4, max(0, $requiredBits - strlen($bits))); $bits .= str_repeat('0', $terminatorLength); echo "Terminator bits: {$terminatorLength}\n"; echo "Bits after terminator: " . strlen($bits) . "\n\n"; // Step 6: Pad to multiple of 8 $remainder = strlen($bits) % 8; if ($remainder !== 0) { $bits .= str_repeat('0', 8 - $remainder); } echo "Bits after padding to 8: " . strlen($bits) . "\n"; // Step 7: Add pad codewords $padBytes = ['11101100', '00010001']; $padIndex = 0; while (strlen($bits) < $requiredBits) { $bits .= $padBytes[$padIndex % 2]; $padIndex++; } echo "Bits after pad codewords: " . strlen($bits) . "\n"; echo "Pad codewords added: {$padIndex}\n\n"; // Step 8: Convert to codewords $codewords = []; for ($i = 0; $i < strlen($bits); $i += 8) { $byte = substr($bits, $i, 8); $codewords[] = bindec($byte); } echo "Generated codewords (" . count($codewords) . "):\n"; echo implode(', ', $codewords) . "\n\n"; // Expected codewords $expected = [64, 180, 132, 84, 196, 196, 242, 5, 116, 245, 174, 59, 64, 109, 236, 233]; echo "Expected codewords (16):\n"; echo implode(', ', $expected) . "\n\n"; // Compare $matches = 0; for ($i = 0; $i < min(count($codewords), count($expected)); $i++) { if ($codewords[$i] === $expected[$i]) { $matches++; } else { echo "❌ Codeword {$i}: got {$codewords[$i]}, expected {$expected[$i]}\n"; echo " Bits: " . substr($bits, $i * 8, 8) . " vs expected: " . str_pad(decbin($expected[$i]), 8, '0', STR_PAD_LEFT) . "\n"; } } if ($matches === count($expected)) { echo "✅ All codewords match!\n"; } else { echo "❌ {$matches}/" . count($expected) . " codewords match\n"; } // Show bit string breakdown echo "\n=== Bit String Breakdown ===\n"; echo "Mode (4): " . substr($bits, 0, 4) . "\n"; echo "Count (8): " . substr($bits, 4, 8) . "\n"; echo "Data (88): " . substr($bits, 12, 88) . "\n"; echo "Terminator + Pad: " . substr($bits, 100) . "\n";