浏览器js和服务端nodejs,普通文本和base64文本互相转换

nodejs 普通文本转base64文本

const base64 = Buffer.from('你好啊,我叫herry菌', 'utf8').toString('base64');
console.log(base64)

 

nodejs base64文本转普通文本

const text = Buffer.from('5L2g5aW95ZWK77yM5oiR5pivaGVycnnoj4w=', 'base64').toString('utf-8');
console.log(text)

 

浏览器js 普通文本转base64文本

//浏览器js 文本转base64
function txtToBase64(text) {
  const data = new TextEncoder().encode(text);//文本转buffer数组
  return btoa(String.fromCharCode.apply(null, data));
}

 

浏览器js base64文本转普通文本

//浏览器js base64转普通文本
function base64ToTxt(base64Text) {
  const decodedText = atob(base64Text);
  return decodeURIComponent(escape(decodedText));
}

 

posted @   herry菌  阅读(37)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2019-06-26 【感想文】不再惧怕被伤害,要正面回应
点击右上角即可分享
微信分享提示