将 Base64 解码为 UTF-8 而不是单字节编码的文本

3 emacs base64

以下是我尝试过的事情:

向缓冲区添加了注释: #-*- coding: utf-8; -*-

M-x M-m cutf-8从列表中选择,然后M-xbase64-decode-region

这是缓冲区显示的内容:\327\252\327\234 \327\220\327\221\327\231\327\221. 它“应该”显示的是?? ????. 源字符串如下所示:16rXnCDXkNeR15nXkQ==

Ste*_*fan 5

缓冲区的编码系统指定从文件读取内容和将内容写入文件时使用的编码系统。IOW,您的“编码:utf-8”仅指示如何解码 ASCII 源字符串(不需要任何特殊解码,因为它是 ASCII,但该 base64 字符串可能被非 ASCII 文本包围)。

你需要的是在打电话decode-coding-region后打电话base64-decode-region

编辑

以下是通讯员defuns:

(defun base64-decode-utf8-region (start end)
  (interactive "r")
  (save-restriction
    (narrow-to-region start end)
    (base64-decode-region (point-min) (point-max))
    (decode-coding-region (point-min) (point-max) 'utf-8)))

(defun base64-encode-utf8-region (start end)
  (interactive "r")
  (save-restriction
    (narrow-to-region start end)
    (encode-coding-region (point-min) (point-max) 'utf-8)
    (base64-encode-region (point-min) (point-max))))
Run Code Online (Sandbox Code Playgroud)