Bin Reader

The app reads .bin files and displays the contents for encryption.
 avatar
user_8806294
actionscript
2 years ago
1.8 kB
4
Indexable
import React, { useState } from 'react';
import { saveAs } from 'file-saver';

function BinFileImporter() {
  const [file, setFile] = useState(null);
  const [fileText, setFileText] = useState('');
  const [isEncrypted, setIsEncrypted] = useState(false);

  const handleFileChange = (event) => {
    setFile(event.target.files[0]);
  };

  const handleFileUpload = async () => {
    const fileReader = new FileReader();
    fileReader.onloadend = (e) => {
      setFileText(e.target.result);
    };
    fileReader.readAsText(file);
  };

  const handleEncryption = () => {
    const encryptedText = fileText.split('').map(char => char.charCodeAt(0)).join(',');
    setFileText(encryptedText);
    setIsEncrypted(true);
  };

  const handleDecryption = () => {
    const decryptedText = fileText.split(',').map(code => String.fromCharCode(code)).join('');
    setFileText(decryptedText);
    setIsEncrypted(false);
  };

  const handleSave = () => {
    const fileType = isEncrypted ? 'text/plain' : 'application/octet-stream';
    const fileName = isEncrypted ? 'encrypted.txt' : 'decrypted.bin';
    const fileBlob = new Blob([fileText], { type: fileType });
    saveAs(fileBlob, fileName);
  };

  const handleClear = () => {
    setFileText('');
    setFile(null);
    setIsEncrypted(false);
  }

  return (
    <div>
      <input type="file" onChange={handleFileChange} accept=".bin" />
      <button onClick={handleFileUpload}>Upload</button>
      <br />
      {!isEncrypted && <button onClick={handleEncryption}>Encrypt to ASCII</button>}
      {isEncrypted && <button onClick={handleDecryption}>Decrypt</button>}
      <br />
      <textarea value={fileText} readOnly />
      <br />
      <button onClick={handleSave}>Save</button>
      <button onClick={handleClear}>Clear</button>
    </div>
  );
}

export default BinFileImporter;
Editor is loading...