index.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Format Converter</title>
  7. </head>
  8. <body>
  9. <h2>Format Converter</h2>
  10. <textarea id="input" rows="4" cols="50" placeholder="输入内容"></textarea><br><br>
  11. <label for="format">选择输入格式:</label>
  12. <select id="inputFormat">
  13. <option value="raw">raw</option>
  14. <option value="hex">hex</option>
  15. <option value="hex-space">hex-space</option>
  16. <option value="hex-prefix">hex-prefix</option>
  17. <option value="hex-comma-prefix">hex-comma-prefix</option>
  18. <option value="binary">binary</option>
  19. <option value="binary-space">binary-space</option>
  20. <option value="binary-prefix">binary-prefix</option>
  21. <option value="base64">base64</option>
  22. </select><br><br>
  23. <label for="format">选择输出格式:</label>
  24. <select id="outputFormat">
  25. <option value="raw">raw</option>
  26. <option value="hex">hex</option>
  27. <option value="hex-space">hex-space</option>
  28. <option value="hex-prefix">hex-prefix</option>
  29. <option value="hex-comma-prefix">hex-comma-prefix</option>
  30. <option value="binary">binary</option>
  31. <option value="binary-space">binary-space</option>
  32. <option value="binary-prefix">binary-prefix</option>
  33. <option value="base64">base64</option>
  34. </select><br><br>
  35. <button onclick="convert()">转换</button><br><br>
  36. <h3>输出:</h3>
  37. <textarea id="output" rows="4" cols="50" readonly></textarea>
  38. <script>
  39. // 将字符串转为 Uint8Array
  40. function stringToBytes(str) {
  41. return new TextEncoder().encode(str);
  42. }
  43. // 将 Uint8Array 转为字符串
  44. function bytesToString(bytes) {
  45. return new TextDecoder().decode(bytes);
  46. }
  47. // 将字符串转为十六进制
  48. function bytesToHex(bytes) {
  49. return Array.from(bytes).map(byte => byte.toString(16).padStart(2, '0').toUpperCase()).join('');
  50. }
  51. // 将十六进制转为 Uint8Array
  52. function hexToBytes(hex) {
  53. const bytes = [];
  54. for (let i = 0; i < hex.length; i += 2) {
  55. bytes.push(parseInt(hex.substr(i, 2), 16));
  56. }
  57. return new Uint8Array(bytes);
  58. }
  59. // 将字符串转为二进制
  60. function bytesToBinary(bytes) {
  61. return Array.from(bytes).map(byte => byte.toString(2).padStart(8, '0')).join('');
  62. }
  63. // 将二进制转为 Uint8Array
  64. function binaryToBytes(binary) {
  65. const bytes = [];
  66. for (let i = 0; i < binary.length; i += 8) {
  67. bytes.push(parseInt(binary.substr(i, 8), 2));
  68. }
  69. return new Uint8Array(bytes);
  70. }
  71. // 转换函数
  72. function convert() {
  73. const input = document.getElementById("input").value.trim();
  74. const inputFormat = document.getElementById("inputFormat").value;
  75. const outputFormat = document.getElementById("outputFormat").value;
  76. let bytes;
  77. // 根据输入格式解析数据
  78. switch (inputFormat) {
  79. case "raw":
  80. bytes = stringToBytes(input);
  81. break;
  82. case "hex":
  83. bytes = hexToBytes(input.replace(/ /g, '').replace(/^0x/g, ''));
  84. break;
  85. case "hex-space":
  86. bytes = hexToBytes(input.replace(/ /g, ''));
  87. break;
  88. case "hex-prefix":
  89. bytes = hexToBytes(input.replace(/0x/g, ''));
  90. break;
  91. case "hex-comma-prefix":
  92. bytes = hexToBytes(input.replace(/0x/g, '').replace(/, /g, ''));
  93. break;
  94. case "binary":
  95. bytes = binaryToBytes(input.replace(/ /g, ''));
  96. break;
  97. case "binary-space":
  98. bytes = binaryToBytes(input.replace(/ /g, ''));
  99. break;
  100. case "binary-prefix":
  101. bytes = binaryToBytes(input.replace(/0b/g, ''));
  102. break;
  103. case "base64":
  104. bytes = new Uint8Array(atob(input).split('').map(char => char.charCodeAt(0)));
  105. break;
  106. default:
  107. alert("不支持的输入格式");
  108. return;
  109. }
  110. // 根据输出格式生成数据
  111. let output;
  112. switch (outputFormat) {
  113. case "raw":
  114. output = bytesToString(bytes);
  115. break;
  116. case "hex":
  117. output = bytesToHex(bytes);
  118. break;
  119. case "hex-space":
  120. output = bytesToHex(bytes).match(/.{1,2}/g).join(' ');
  121. break;
  122. case "hex-prefix":
  123. output = bytesToHex(bytes).match(/.{1,2}/g).map(byte => '0x' + byte).join(' ');
  124. break;
  125. case "hex-comma-prefix":
  126. output = bytesToHex(bytes).match(/.{1,2}/g).map(byte => '0x' + byte).join(', ');
  127. break;
  128. case "binary":
  129. output = bytesToBinary(bytes);
  130. break;
  131. case "binary-space":
  132. output = bytesToBinary(bytes).match(/.{1,8}/g).join(' ');
  133. break;
  134. case "binary-prefix":
  135. output = bytesToBinary(bytes).match(/.{1,8}/g).map(byte => '0b' + byte).join(' ');
  136. break;
  137. case "base64":
  138. output = btoa(String.fromCharCode(...bytes));
  139. break;
  140. default:
  141. alert("不支持的输出格式");
  142. return;
  143. }
  144. // 显示输出结果
  145. document.getElementById("output").value = output;
  146. }
  147. </script>
  148. </body>
  149. </html>