https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding

In JavaScript there are two functions respectively for decoding and encoding base64 strings:

工作场景,客户端HTML片段的保存与再现。

The "Unicode Problem"

Since DOMStrings are 16-bit-encoded strings, in most browsers calling window.btoa on a Unicode string will cause a Character Out Of Range exception if a character exceeds the range of a 8-bit byte (0x00~0xFF). There are two possible methods to solve this problem:

  • the first one is to escape the whole string (with UTF-8, see encodeURIComponent) and then encode it;
  • the second one is to convert the UTF-16 DOMString to an UTF-8 array of characters and then encode it.

Here are the two possible methods.

https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#Solution_1_–_escaping_the_string_before_encoding_it

https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#Solution_2_–_rewrite_the_DOMs_atob()_and_btoa()_using_JavaScript's_TypedArrays_and_UTF-8

其它相关帖子:

https://blog.coding.net/blog/resolve-atob-decode-chinese-character-outputting-messy-code-problem-in-javascript

http://levy.work/2017-03-24-black-magic-js-atob-with-utf8/

最新文章

  1. 如何学习Oracle
  2. 【转】Python资源 转自 dylanninin.com
  3. 用javascript协助导入图片
  4. U8记账凭证修改方法汇总
  5. android 14 进度条和拖动条
  6. 爬虫day 04(通过登录去爬虫 解决django的csrf_token)
  7. Asp.Net Web API(一)
  8. Android 常用的ORM框架详解
  9. Android实训案例(一)——计算器的运算逻辑
  10. 显示开机信息-dmesg
  11. [转] OpenStack — nova image-create, under the hood
  12. 136.137.260. Single Number && 位运算
  13. 官方解析Cookies和Session的区别
  14. 使用requests库提交multipart/form-data 格式的请求
  15. JS结合a标签的使用
  16. 【转载】linux下升级npm以及node
  17. css基础示例代码
  18. AsyncTask、HandlerThread、IntentService和线程池
  19. 铁乐学Python_day03-字符串常用操作方法
  20. 主机名/etc/hosts文件的作用

热门文章

  1. Centos7下安装docker(1)
  2. Spark机器学习中ml和mllib中矩阵、向量
  3. 剑指offer.在O(1)时间内删除链表节点
  4. PAT A1104 Sum of Number Segments (20 分)——数学规律,long long
  5. 关于for,while,dowhile效率测试
  6. oracle 把查询结果插入到表中几种方式
  7. 随机指定范围内N个不重复的数
  8. 在IIS上部署你的ASP.NET Core项目 (转载)
  9. 【LeetCode232】 Implement Queue using Stacks★
  10. Python学习总结 05 pandas