You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript数组映射解码器无法识别8位数字串问题求助

Hey there! Let's fix your decrypt function step by step. The main issues are how you're handling the 8-digit code strings, the lookup map's keys, and the way you're iterating through the input text. Here's what's going wrong and how to fix it:

Key Issues in Your Current Code

  • map2 uses numeric keys (octal problem): When you write keys like 01011010 in JavaScript, they're treated as octal (base 8) numbers, not 8-digit decimal strings. This breaks the lookup because the encrypted output is string-based, not octal numbers.
  • Decrypt function iterates over single characters: Right now you're checking each character one by one, but you need to grab 8-digit chunks (plus handle spaces as separators).
  • Switch case in Decrypt is targeting letters instead of code strings: Your decrypt switch is checking for "A", "B", etc., but the input here should be the 8-digit codes from encryption.
  • Typo in loop variable: You have newText2 += DecryptedText[i]; but the loop uses o as the variable, which will cause an error.

Fixed Code

First, update map2 to use string keys (wrap each 8-digit code in quotes) so JavaScript treats them as literal strings:

var map = { 
  A: '01011010 ', B: '01011001 ', C: '01011000 ', D: '01010111 ', 
  E: '01010110 ', F: '01010101 ', G: '01010100 ', H: '01010011 ', 
  I: '01010010 ', J: '01010001 ', K: '01010000 ', L: '01001111 ', 
  M: '01001110 ', N: '01001101 ', O: '01001100 ', P: '01001011 ', 
  Q: '01001010 ', R: '01001001 ', S: '01001000 ', T: '01000111 ', 
  U: '01000110 ', V: '01000101 ', W: '01000100 ', X: '01000011 ', 
  Y: '01000010 ', Z: '01000001 ' 
}; 

// Updated map2 with string keys
var map2 = { 
  '01011010': "A", '01011001': "B", '01011000': "C", '01010111': "D", 
  '01010110': "E", '01010101': "F", '01010100': "G", '01010011': "H", 
  '01010010': "I", '01010001': "J", '01010000': "K", '01001111': "L", 
  '01001110': "M", '01001101': "N", '01001100': "O", '01001011': "P", 
  '01001010': "Q", '01001001': "R", '01001000': "S", '01000111': "T", 
  '01000110': "U", '01000101': "V", '01000100': "W", '01000011': "X", 
  '01000010': "Y", '01000001': "Z" 
};

Then rewrite the Encrypt function to avoid the long switch (you can just use the map object directly, which is cleaner):

function Encrypt() {
  var inputEncrypted = document.getElementById("Encrypted");
  var EncryptedText = inputEncrypted.value;
  var newText = "";
  
  for(var i=0; i < EncryptedText.length; i++) {
    var char = EncryptedText[i];
    // Use the map to get the code, or keep the original char if not in map
    newText += map[char] || char;
  }
  
  inputEncrypted.value = newText;
}

And the fixed Decrypt function, which handles 8-digit chunks and spaces properly:

function Decrypt() {
  var inputDecrypted = document.getElementById("Decrypted");
  var DecryptedText = inputDecrypted.value;
  var newText2 = "";
  var i = 0;
  
  while (i < DecryptedText.length) {
    // Handle spaces first
    if (DecryptedText[i] === " ") {
      newText2 += " ";
      i++;
    } else {
      // Grab the next 8 characters (the code)
      var code = DecryptedText.slice(i, i + 8);
      // Look up the code in map2, or keep the code if not found
      newText2 += map2[code] || code;
      // Move 9 steps forward (8 digits + 1 space after each code)
      i += 9;
    }
  }
  
  inputDecrypted.value = newText2;
}

Explanation of Changes

  • String keys in map2: Ensures we're matching the exact string output from encryption, no octal conversion issues.
  • Chunk-based decryption: Instead of looping through single characters, we grab 8-digit chunks (since each letter maps to 8 digits plus a space). The i +=9 skips the space after each code.
  • Simplified Encrypt function: Uses the map object directly instead of a huge switch statement, making the code shorter and easier to maintain.
  • Fixed loop typo: Removed the incorrect DecryptedText[i] reference and used proper iteration logic.

内容的提问来源于stack exchange,提问作者Teo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:35:02