当前位置:网站首页>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 从入门到精通”
边栏推荐
- Configuration and use of virtualservice, gateway and destinationrule of istio III
- QT notes - Q_ Q and Q_ D learning
- File upload error: current request is not a multipart request
- [mathematical modeling - Summary of planning model] | matlab solution
- 论文精读-YOLOv1:You Only Look Once:Unified, Real-Time Object Detection
- ext4、ntfs、xfs、btrfs、zfs、f2fs和reiserFS性能对比
- Canvas -- draw curve -- wall clock, pie chart, five pointed star
- LoRa和NB-IOT可用用在哪些地方
- Tf.constant usage
- Leetcode-169. most elements
猜你喜欢

Navicat连接云端服务器上的MySQL数据库

Easyexcel sets row hiding to solve the problem of sethidden (true) invalidation

Dominate the salary list! What industry has a "money" path?

What are you interviewing for in a big factory? It's worth watching (I)

C language functions (2)

PHP连接mysql数据库,数据库连接静态工具类,简化连接。

Alibaba Sentinel - 集群流量控制

Leetcode · daily question · sword finger offer | | 115. reconstruction sequence · topological sorting

使用anaconda配置gpu版本的tensorflow(30系列以下显卡)
![[stl] priority queue priority_ queue](/img/79/d13913cbb9d98f936a9501633b38bf.png)
[stl] priority queue priority_ queue
随机推荐
Understand preloading and lazy loading, and learn slow animation
Can UDP and TCP use the same port?
阿里二面:千万级数据量的表,快速查询如何进行?
全校软硬件基础设施一站式监控 ,苏州大学以时序数据库替换 PostgreSQL
Portable power fast charging scheme 30W automatic pressure rise and fall PD fast charging
Tf.constant usage
Use VRRP technology to realize gateway equipment redundancy, with detailed configuration experiments
Docker installs redis!!! (including detailed illustration of each step) actual combat
[tensorflow & pytorch] image data enhancement API
大厂面试都面试些啥,看了不亏(一)
Summary of basic knowledge of C language pointer (I)
Why are more and more users of Bing search?
78. Subset
Hurry in!!! Write a number guessing game with dozens of lines of code based on the basic knowledge of C language
让百度收录,爬虫自己网站
Dominate the salary list! What industry has a "money" path?
Canvas -- draw curve -- wall clock, pie chart, five pointed star
Canvas - ECG design and how to clean the canvas
2022-07-21 group 4 polymorphism
Opencv error: (parameter or structure field)) unrecognized or unsupported array type in functon 'cvgetmat‘