user797257
user797257

Reputation:

Decode Base64 into UTF-8 rather then single byte encoded text

Here are things that I've tried:

Added annotation to the buffer: #-*- coding: utf-8; -*-

M-x M-m c, select utf-8 from the list, then M-xbase64-decode-region.

Here's what the buffer shows: \327\252\327\234 \327\220\327\221\327\231\327\221. What it "should" be showing is תל אביב. The source string looks like this: 16rXnCDXkNeR15nXkQ==

Upvotes: 4

Views: 1080

Answers (1)

Stefan
Stefan

Reputation: 28531

The buffer's coding system specifies the coding system used when reading the content from a file and when writing the content to a file. IOW, your "coding: utf-8" thingy only indicates how to decode the ASCII source string (which does not require any special decoding since it's ASCII, but that base64 string could be surrounded by non-ASCII text).

What you need is to call decode-coding-region after having called base64-decode-region.

Edit

Here are correspondent 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))))

Upvotes: 5

Related Questions