| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Format Converter</title>
- </head>
- <body>
- <h2>Format Converter</h2>
- <textarea id="input" rows="4" cols="50" placeholder="输入内容"></textarea><br><br>
- <label for="format">选择输入格式:</label>
- <select id="inputFormat">
- <option value="raw">raw</option>
- <option value="hex">hex</option>
- <option value="hex-space">hex-space</option>
- <option value="hex-prefix">hex-prefix</option>
- <option value="hex-comma-prefix">hex-comma-prefix</option>
- <option value="binary">binary</option>
- <option value="binary-space">binary-space</option>
- <option value="binary-prefix">binary-prefix</option>
- <option value="base64">base64</option>
- </select><br><br>
- <label for="format">选择输出格式:</label>
- <select id="outputFormat">
- <option value="raw">raw</option>
- <option value="hex">hex</option>
- <option value="hex-space">hex-space</option>
- <option value="hex-prefix">hex-prefix</option>
- <option value="hex-comma-prefix">hex-comma-prefix</option>
- <option value="binary">binary</option>
- <option value="binary-space">binary-space</option>
- <option value="binary-prefix">binary-prefix</option>
- <option value="base64">base64</option>
- </select><br><br>
- <button onclick="convert()">转换</button><br><br>
- <h3>输出:</h3>
- <textarea id="output" rows="4" cols="50" readonly></textarea>
- <script>
- // 将字符串转为 Uint8Array
- function stringToBytes(str) {
- return new TextEncoder().encode(str);
- }
- // 将 Uint8Array 转为字符串
- function bytesToString(bytes) {
- return new TextDecoder().decode(bytes);
- }
- // 将字符串转为十六进制
- function bytesToHex(bytes) {
- return Array.from(bytes).map(byte => byte.toString(16).padStart(2, '0').toUpperCase()).join('');
- }
- // 将十六进制转为 Uint8Array
- function hexToBytes(hex) {
- const bytes = [];
- for (let i = 0; i < hex.length; i += 2) {
- bytes.push(parseInt(hex.substr(i, 2), 16));
- }
- return new Uint8Array(bytes);
- }
- // 将字符串转为二进制
- function bytesToBinary(bytes) {
- return Array.from(bytes).map(byte => byte.toString(2).padStart(8, '0')).join('');
- }
- // 将二进制转为 Uint8Array
- function binaryToBytes(binary) {
- const bytes = [];
- for (let i = 0; i < binary.length; i += 8) {
- bytes.push(parseInt(binary.substr(i, 8), 2));
- }
- return new Uint8Array(bytes);
- }
- // 转换函数
- function convert() {
- const input = document.getElementById("input").value.trim();
- const inputFormat = document.getElementById("inputFormat").value;
- const outputFormat = document.getElementById("outputFormat").value;
- let bytes;
- // 根据输入格式解析数据
- switch (inputFormat) {
- case "raw":
- bytes = stringToBytes(input);
- break;
- case "hex":
- bytes = hexToBytes(input.replace(/ /g, '').replace(/^0x/g, ''));
- break;
- case "hex-space":
- bytes = hexToBytes(input.replace(/ /g, ''));
- break;
- case "hex-prefix":
- bytes = hexToBytes(input.replace(/0x/g, ''));
- break;
- case "hex-comma-prefix":
- bytes = hexToBytes(input.replace(/0x/g, '').replace(/, /g, ''));
- break;
- case "binary":
- bytes = binaryToBytes(input.replace(/ /g, ''));
- break;
- case "binary-space":
- bytes = binaryToBytes(input.replace(/ /g, ''));
- break;
- case "binary-prefix":
- bytes = binaryToBytes(input.replace(/0b/g, ''));
- break;
- case "base64":
- bytes = new Uint8Array(atob(input).split('').map(char => char.charCodeAt(0)));
- break;
- default:
- alert("不支持的输入格式");
- return;
- }
- // 根据输出格式生成数据
- let output;
- switch (outputFormat) {
- case "raw":
- output = bytesToString(bytes);
- break;
- case "hex":
- output = bytesToHex(bytes);
- break;
- case "hex-space":
- output = bytesToHex(bytes).match(/.{1,2}/g).join(' ');
- break;
- case "hex-prefix":
- output = bytesToHex(bytes).match(/.{1,2}/g).map(byte => '0x' + byte).join(' ');
- break;
- case "hex-comma-prefix":
- output = bytesToHex(bytes).match(/.{1,2}/g).map(byte => '0x' + byte).join(', ');
- break;
- case "binary":
- output = bytesToBinary(bytes);
- break;
- case "binary-space":
- output = bytesToBinary(bytes).match(/.{1,8}/g).join(' ');
- break;
- case "binary-prefix":
- output = bytesToBinary(bytes).match(/.{1,8}/g).map(byte => '0b' + byte).join(' ');
- break;
- case "base64":
- output = btoa(String.fromCharCode(...bytes));
- break;
- default:
- alert("不支持的输出格式");
- return;
- }
- // 显示输出结果
- document.getElementById("output").value = output;
- }
- </script>
- </body>
- </html>
|