reddit / emacs

How to use encode-coding-string function

milouse · 2022-09-30 20:29
Hello every one, I didn’t pay attention until recently, but I keep seeing the following warning while byte-compiling my preference files: ``` Warning: ‘string-as-unibyte’ is an obsolete function (as of 26.1); use ‘encode-coding-string’. ``` I use the following code to convert arbitrary text (usually utf-8, but may be something else): ``` (base64-encode-string (string-as-unibyte (buffer-string)) 'no-line-break) ``` I did look at the `encode-coding-string`, but what bother me is that it seems that I’m obliged to set the original `coding-system`. But in my case it can change from time to time. Is there a way to keep the same "magical" behavior of `string-as-unibyte` function, with which I didn’t have to assume a possibly wrong source coding system? Am I correct if I say it’d be the `undecided` coding system? As in: ``` (base64-encode-string (encode-coding-string (buffer-string) 'undecided) 'no-line-break) ``` As I’m reading the whole buffer content, is there a (local) variable holding the current buffer coding system? Thank you very much!
milouse · 2022-09-30 20:41
Mmmmmh I just read that 'undecided should not be used with `encode-coding-string`. Is it better to use: ``` (base64-encode-string (encode-coding-string (buffer-string) (select-safe-coding-system (point-min) (point-max))) 'no-line-break) ``` It seems to do what I want, but I wonder if I didn’t completely miss the point?
eli-zaretskii · 2022-10-01 07:14
Just use UTF-8, it should do what you want. No need for any complications.