Advertisement
Thunder-Menu

EncodeDecodeFileToJson.html

Oct 9th, 2023
961
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5.27 KB | Source Code | 0 0
  1. <!DOCTYPE html>
  2. <html lang="fr">
  3.  
  4. <head>
  5.     <meta charset="UTF-8">
  6.     <title>Encoder & Décoder des Données Binaires</title>
  7. </head>
  8.  
  9. <body>
  10.  
  11.    <select id="mode" onchange="changeMode()">
  12.        <option value="encode">Encoder</option>
  13.        <option value="decode">Décoder</option>
  14.    </select>
  15.    <br>
  16.    <input type="file" id="fileInput" onchange="loadFile(event)">
  17.    <br>
  18.    <button onclick="processData()">Traiter les données</button>
  19.  
  20.    <script>
  21.        let fileContent = "";
  22.         let fileName = "";
  23.         let fileExtension = "";
  24.  
  25.         function changeMode() {
  26.             const mode = document.getElementById("mode").value;
  27.             if (mode === "encode") {
  28.                 document.getElementById("fileInput").setAttribute("accept", "*/*");
  29.             } else {
  30.                 document.getElementById("fileInput").setAttribute("accept", ".json");
  31.             }
  32.         }
  33.  
  34.         function loadFile(event) {
  35.             const reader = new FileReader();
  36.             reader.onload = function(e) {
  37.                 fileContent = e.target.result;
  38.             }
  39.  
  40.             if (document.getElementById("mode").value === "encode") {
  41.                 fileName = event.target.files[0].name.split('.').slice(0, -1).join('.');
  42.                 fileExtension = "." + event.target.files[0].name.split('.').pop();
  43.                 reader.readAsArrayBuffer(event.target.files[0]);
  44.             } else {
  45.                 reader.readAsText(event.target.files[0]);
  46.             }
  47.         }
  48.  
  49.         function processData() {
  50.             const mode = document.getElementById("mode").value;
  51.             if (mode === "encode") {
  52.                 encodeData();
  53.             } else {
  54.                 decodeData();
  55.             }
  56.         }
  57.  
  58.         function encodeData() {
  59.             const dataView = new DataView(fileContent);
  60.             let hexString = '';
  61.             for (let i = 0; i < dataView.byteLength; i++) {
  62.                hexString += dataView.getUint8(i).toString(16).padStart(2, '0');
  63.            }
  64.            const binaryData = hexString.split('').map((hex) => parseInt(hex, 16).toString(2).padStart(4, '0')).join('');
  65.  
  66.             const positions = {
  67.                 lastZeroPosition: binaryData.lastIndexOf('0'),
  68.                 fileName: fileName,
  69.                 fileExtension: fileExtension,
  70.                 sequences: []
  71.             };
  72.  
  73.             let sequenceCount = 0;
  74.             let startPosition = 0;
  75.             let lastPosition = 0;
  76.  
  77.             for (let i = 0; i < binaryData.length; i++) {
  78.                if (binaryData[i] === '1') {
  79.                    if (sequenceCount === 0) startPosition = i;
  80.                    sequenceCount++;
  81.                    if (i === binaryData.length - 1 || binaryData[i + 1] === '0') {
  82.                        let relativeStart = startPosition - lastPosition;
  83.                        if (sequenceCount > 1) {
  84.                             positions.sequences.push(relativeStart + "-" + sequenceCount);
  85.                         } else {
  86.                             positions.sequences.push(relativeStart.toString());
  87.                         }
  88.                         lastPosition = i + 1; // to jump correctly to the next sequence
  89.                         sequenceCount = 0;
  90.                     }
  91.                 }
  92.             }
  93.  
  94.             const blob = new Blob([JSON.stringify(positions, null, 2)], { type: "application/json" });
  95.             const url = URL.createObjectURL(blob);
  96.             const a = document.createElement("a");
  97.             a.href = url;
  98.             a.download = "data.json";
  99.             document.body.appendChild(a);
  100.             a.click();
  101.             document.body.removeChild(a);
  102.         }
  103.  
  104.         function decodeData() {
  105.             const positions = JSON.parse(fileContent);
  106.             const binaryData = Array(positions.lastZeroPosition + 1).fill('0');
  107.  
  108.             let currentPosition = 0;
  109.             for (const seq of positions.sequences) {
  110.                 if (seq.includes('-')) {
  111.                     const [gap, count] = seq.split('-').map(Number);
  112.                     currentPosition += gap;
  113.                     for (let j = 0; j < count; j++) {
  114.                        binaryData[currentPosition + j] = '1';
  115.                    }
  116.                    currentPosition += count;
  117.                } else {
  118.                    currentPosition += Number(seq);
  119.                    binaryData[currentPosition] = '1';
  120.                    currentPosition++;
  121.                }
  122.            }
  123.  
  124.            const hexString = binaryData.join('').match(/.{4}/g).map((bin) => parseInt(bin, 2).toString(16)).join('');
  125.             const output = new Uint8Array(hexString.length / 2);
  126.             for (let i = 0; i < hexString.length; i += 2) {
  127.                output[i / 2] = parseInt(hexString.substr(i, 2), 16);
  128.            }
  129.  
  130.            const blob = new Blob([output.buffer], { type: "application/octet-stream" });
  131.            const url = URL.createObjectURL(blob);
  132.            const a = document.createElement("a");
  133.            a.href = url;
  134.            a.download = positions.fileName + positions.fileExtension;
  135.            document.body.appendChild(a);
  136.            a.click();
  137.            document.body.removeChild(a);
  138.        }
  139.    </script>
  140.  
  141. </body>
  142.  
  143. </html>
  144.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement