前端JS实现base64解码&十六进制解码&url解码

前端js实现base64解码

a = atob('ZXZhbCUyOCUyNF9QT1NUJTVCJTI3eCUyNyU1RCUyOQ==')
decodeURIComponent(a)
最主要函数atob

前端js实现十六进制解码
function hexToUtf8(s)
{
  return decodeURIComponent(
     s.replace(/\s+/g, '') // remove spaces
      .replace(/[0-9a-f]{2}/g, '%$&') // add '%' before each 2 characters
  );
}

const utf8encoder = new TextEncoder();

function utf8ToHex(s)
{
  const rb = utf8encoder.encode(s);
  let r = '';
  for (const b of rb) {
    r += ('0' + b.toString(16)).slice(-2);
  }
  return r;
}


var hex = "d7a452656c6179204f4e214f706572617465642062792030353232";

var utf8 = hexToUtf8(hex);
var hex2 = utf8ToHex(utf8);

console.log("Hex: " + hex);
console.log("UTF8: " + utf8);
console.log("Hex2: " + hex2);
console.log("Is conversion OK: " + (hex == hex2));

前端js实现url解码

decodeURIComponent('..%2F..%2F..%2F..%2F..%2Fetc%2Fpasswd')

posted @ 2022-11-22 17:05  前女友破鞋  阅读(1550)  评论(0编辑  收藏  举报