1

I have a string "\\303\\273" that I want to decode into û.

It's known that (decode-coding-string "\303\273" 'utf-8) evals to û.

However, I'm not able to translate "\\303\\273" into "\303\273" with replace-regexp-in-string.

Question: How to decode "\\303\\273" into û?

2

"\303\273" is 2 bytes and "\\303\\273" is 8 bytes, the problem with replace-regexp-in-string is it eagerly converts \303 to à and \273 to ».

(replace-regexp-in-string
 (rx "\\" (group (1+ (in "0-7"))))
 (lambda (s)
   (string
    (string-to-number
     (match-string 1 s)
     8)))
 "\\303\\273")
;; => "û"

Here is a way without replace-regexp-in-string. Note that you need unibyte-string not string, since string will have the same problem as replace-regexp-in-string.

(decode-coding-string
 (apply
  #'unibyte-string ;; string does not work
  (mapcar
   (lambda (s)
     (string-to-number
      (substring s 1)
      8))
   (seq-partition "\\303\\273" 4)))
 'utf-8)
;; => "û"
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.