当前位置:网站首页>JS base64编码和解码
JS base64编码和解码
2022-07-26 03:33:00 【学亮编程手记】
Base64 是一种编码方式,可以将任意字符(包括二进制字符流)转成可打印字符。
JavaScript
定义了两个与 Base64 相关的全局方法。
- btoa():字符串或二进制值转为 Base64 编码。
- atob():把 Base64 编码转为原来的字符。
Base64 方法不能够操作非 ASCII 字符。
示例
要将非 ASCII 码字符转为 Base64 编码,必须使用上一节介绍的方法把 Unicode 双字节字符串转换为 ASCII 字符表示,再使用这两个方法。
encodeURIComponent和decodeURIComponent非必需。只在URI传参时才用到。
也就是:btoa和atob可以直接接收一个字符串参数。
function b64Encode (str) {
return btoa(encodeURIComponent(str));
}
function b64Decode (str) {
return decodeURIComponent(atob(str));
}
var b = b64Encode('JavaScript 从入门到精通');
var a = b64Decode(b);
console.log(b); //返回SmF2YVNjcmlwdCVFNCVCQiU4RSVFNSU4NSVBNFOSU5NyVBOCVFNSU4OCVCMCVFNyVCMiVCRSVFOSU4MCU5QQ==
console.log(a); //返回“JavaScript 从入门到精通”
边栏推荐
- Is the galaxy VIP account opened by qiniu safe?
- 2020 AF-RCNN: An anchor-free convolutional neural network for multi-categoriesagricultural pest det
- 使用VRRP技术实现网关设备冗余,附详细配置实验
- [class and object instances in kotlin]
- Alibaba Sentinel - 集群流量控制
- redis集群的三种方式
- Three solutions: when clicking the user to exit the login, press the back button of the browser, and you can still see the previous login page.
- ELS modify cursor, modify Icon
- Unity quickly builds urban scenes
- [Yuri crack man] brings you easy understanding - deep copy and shallow copy
猜你喜欢

使用anaconda配置gpu版本的tensorflow(30系列以下显卡)

Course notes of single chip microcomputer principle and interface technology for migrant workers majoring in electronic information engineering

File upload error: current request is not a multipart request
![[mathematical modeling - Summary of planning model] | matlab solution](/img/b0/a4d33d7d7b605b7cc6149b59b55fb8.png)
[mathematical modeling - Summary of planning model] | matlab solution

Illustration leetcode - 5. Longest palindrome substring (difficulty: medium)

多商户商城系统功能拆解15讲-平台端会员标签

使用VRRP技术实现网关设备冗余,附详细配置实验

Use VRRP technology to realize gateway equipment redundancy, with detailed configuration experiments

离线数据仓库从0到1-阶段二软件安装

What are you interviewing for in a big factory? It's worth watching (I)
随机推荐
Sentinel vs Hystrix 到底怎么选?
C language functions (2)
文件上传报错:Current request is not a multipart request
Graduation season & harvest season, leave your beautiful moments
MPLS basic experiment configuration
Canvas - drawing pictures - dynamic drawing production
Dominate the salary list! What industry has a "money" path?
QT notes - Q_ Q and Q_ D learning
Bing(必应)搜索,为什么用户越来越多?
【创建交互式 Dice Roller 应用】
redis集群的三种方式
Efficient Video Instance Segmentation via Tracklet Query and Proposal
ext4、ntfs、xfs、btrfs、zfs、f2fs和reiserFS性能对比
Classic interview questions -- three characteristics of OOP language
Mbr3045ct Schottky diode, mbr0100, mbr2060ct diode parameters
DDD落地的那叫一个高级
【 Kotlin 中的类和对象实例】
easyExcel设置行隐藏,解决setHidden(true)失效问题
论文精读-YOLOv1:You Only Look Once:Unified, Real-Time Object Detection
c语言指针基本知识要点总结(一)