Encode "ä", "ö", "ü" and "ß" in scheme (file-output-port)

49 Views Asked by At

I need to work with Files (Input and Output) containing the Symbols ÄäÖöÜüß and €. So it is possible to use the transcoders with the latin-1-codec or the utf-8-codec (utf-8 is perhaps the better option). I don't want to use the utf-16-codec. While the File-Input-Port works well (Encode "ä", "ö", "ü" and "ß" in scheme (get german text from file)), I can't get the File-Output-Port working.

(define path ... <path to a txt-file containing this text: "äöüß">)

;; Input-Port works fine: 
(define fip (open-file-input-port
         path
         (file-options)
         (buffer-mode block)
         (make-transcoder (utf-8-codec))))

(get-string-all fip) ;; returns: "äöüß"

;; But Output-Port don't:
(define fop (open-file-output-port
         path
         (file-options no-fail)
         (buffer-mode block)
         (make-transcoder (utf-8-codec))))

(put-string fop "äöüß")
(close-port fop) ;; it stores just strange symbols in the txt-file

I don't know why it is not working. Trying to understand the problem i tried this:

(display (string->utf8 "ä")) -> #vu8 (239 191 189)
(utf8->string (string->utf8 "ä")) ;; returns a strange symbol
(utf8->string (string->utf8 "a")) ;; returns "a"

(utf8->string 
 (let-values (((p f) (open-bytevector-output-port
              (make-transcoder (utf-8-codec)))))
   (put-string p "a ä")
   (f))) ;; returns "a <strange symbol>"

I can read the Symbols "äöüß" but I can't write them via ports.

1

There are 1 best solutions below

0
Sem-Joe On

OK. Got it! The Problem was my IDE (emacs). The Solution was to include the following code to the .emacs-file:

(prefer-coding-system 'utf-8)
(set-default-coding-systems 'utf-8)

See also: https://goyoambrosio.com/2018/06/Dealing-with-utf-8-in-Emacs/