美文网首页
零宽字符实现与应用

零宽字符实现与应用

作者: 是ADI呀 | 来源:发表于2022-12-02 10:58 被阅读0次

    Hello!

    copy: ADI
    paste:

    encrypt

    简易实现

    const zeroPad = (num) => "00000000".slice(String(num).length) + num;
    const textToBinary = (username) =>
      username
        .split("")
        .map((char) => zeroPad(char.charCodeAt(0).toString(2)))
        .join(" ");
    
    // 加密字典
    const binaryToZeroWidth = (binary) =>
      binary
        .split("")
        .map((binaryNum) => {
          const num = parseInt(binaryNum, 10);
          if (num === 1) {
            return "";
          } else if (num === 0) {
            return "";
          }
          return "";
        })
        .join("");
    
    // Tests
    // const textToBinaryADI = textToBinary("ADI"); // 二进制
    // console.log(`ADI-LOG => textToBinaryADI`, textToBinaryADI);
    // const binaryToZeroWidthADI = binaryToZeroWidth(textToBinaryADI); // 零宽字符
    // console.log(`ADI-LOG => binaryToZeroWidthADI`, binaryToZeroWidthADI, binaryToZeroWidthADI.length);
    // // binaryToZeroWidthADI => 
    
    // 解密字典
    const zeroWidthToBinary = (string) =>
      string
        .split("")
        .map((char) => {
          if (char === "") {
            return "1";
          } else if (char === "") {
            return "0";
          }
          return " ";
        })
        .join("");
    
    // Tests
    // const zeroWidthToBinaryADI = zeroWidthToBinary(binaryToZeroWidthADI);
    // console.log(`ADI-LOG => zeroWidthToBinaryADI`, zeroWidthToBinaryADI);
    
    const binaryToText = (string) =>
      string
        .split(" ")
        .map((num) => String.fromCharCode(parseInt(num, 2)))
        .join("");
    
    // Tests
    // const binaryToTextADI = binaryToText(zeroWidthToBinaryADI);
    // console.log(`ADI-LOG => binaryToTextADI`, binaryToTextADI);
    
    

    资料

    相关文章

      网友评论

          本文标题:零宽字符实现与应用

          本文链接:https://www.haomeiwen.com/subject/iuyefdtx.html