help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to replace a string to Unicode escape?


From: harven
Subject: Re: How to replace a string to Unicode escape?
Date: Mon, 16 Nov 2009 22:22:06 +0100
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (darwin)

ptmono <ptmono@gmail.com> writes:

> A program returns a unicode value "%uc0ac%ub791". The value can be the
> combination of unicode with ascii. For example "%uc0ac%ub791 sarang
> %uc0ac%ub791".
>
> The function string-to-multibyte can be used to return the string.
>
> ,----
> | (string-to-multibyte "\uc0ac\ub791 sarang \uc0ac\ub791")
> | "사랑 sarang 사랑"
> `----
>
> The *problem* is that how to replace "%" to "\" or "%u" to "\u".

Here is a ugly hack (use at your own risk)

(defun my-ucs (arg)
  "return unicode character \\uxxxx from string \"%uxxxx\" or return \"\".
   probably fails badly if xxx is not a valid hex code"
    (or 
      (char-to-string
        (decode-char 'ucs
               (string-to-number
                   (substring arg 2) 16)))
      ""))

(replace-regexp-in-string 
     "%u...."
     'my-ucs
     "%uc0ac%ub791 sarang %uc0ac%ub791")

 -->  "사랑 sarang 사랑"

Cheers


reply via email to

[Prev in Thread] Current Thread [Next in Thread]