你可以使用以下方法将文本/rtf数据转换为16进制:

function textToHex(text) {
  let hex = '';
  for (let i = 0; i < text.length; i++) {
    hex += text.charCodeAt(i).toString(16);
  }
  return hex;
}

const rtfData = "RTF data";
const hexData = textToHex(rtfData);
console.log(hexData);

然后,你可以使用以下方法将16进制数据转换为base64:

function hexToBase64(hex) {
  const binary = hex.match(/.{1,2}/g).map(byte => String.fromCharCode(parseInt(byte, 16))).join('');
  return btoa(binary);
}

const base64Data = hexToBase64(hexData);
console.log(base64Data);

请注意,这些方法假设你正在运行在浏览器环境中。如果你在Node.js环境中使用,你需要使用Buffer对象来进行base64编码,而不是使用btoa函数。

function hexToBase64(hex) {
  const binary = hex.match(/.{1,2}/g).map(byte => String.fromCharCode(parseInt(byte, 16))).join('');
  return Buffer.from(binary, 'binary').toString('base64');
}

希望这可以帮助到你!

js 把textrtf数据转换16进制通过16进制转base64

原文地址: http://www.cveoy.top/t/topic/ii5e 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录