将Unicode(UTF-8)代码点转换为字节

时间:2012-06-18 14:43:01

标签: emacs unicode utf-8 elisp

我去搜索C源代码,但我找不到这个功能,我真的不想自己写一个,因为它绝对必须在那里。

详细说明:Unicode点表示为U + ######## - 这很容易获得,我需要的是字符写入文件的格式(例如)。 Unicode代码点转换为字节,使得最右边的字节的7位写入第一个字节,然后将下一个位的6位写入下一个字节,依此类推。 Emacs肯定知道怎么做,但是我无法找到将UTF-8编码字符串的字节序列作为字节序列(每个包含8位)。

get-bytemultybite-char-to-unibyte等函数仅适用于可以使用不超过8位表示的字符。我需要get-byte所做的相同的事情,但是对于多字节字符,所以我不会接收整数0..256的整数,而是接收整数0..256或单个长整数0..2的向量^ 32

修改

万一以后有人会需要这个:

(defun haxe-string-to-x-string (s)
  (with-output-to-string
    (let (current parts)
      (dotimes (i (length s))
        (if (> 0 (multibyte-char-to-unibyte (aref s i)))
            (progn
              (setq current (encode-coding-string
                             (char-to-string (aref s i)) 'utf-8))
              (dotimes (j (length current))
                (princ (format "\\x%02x" (aref current j)))))
          (princ (format "\\x%02x" (aref s i))))))))

1 个答案:

答案 0 :(得分:5)

encode-coding-string可能是您正在寻找的内容:

*** Welcome to IELM ***  Type (describe-mode) for help.
ELISP> (encode-coding-string "eĥoŝanĝo ĉiuĵaŭde" 'utf-8)
"e\304\245o\305\235an\304\235o \304\211iu\304\265a\305\255de"

它返回一个字符串,但您可以使用aref

访问各个字节
ELISP> (aref (encode-coding-string "eĥoŝanĝo ĉiuĵaŭde" 'utf-8) 1)
196
ELISP> (format "%o" 196)
"304"

如果您不介意使用cl功能,concatenate是您的朋友:

ELISP> (concatenate 'list (encode-coding-string "eĥoŝanĝo ĉiuĵaŭde" 'utf-8))
(101 196 165 111 197 157 97 110 196 157 111 32 196 137 105 117 196 181 97 197 173 100 101)
相关问题