2012-06-18 5 views
6

Cソースの検索までは行っていましたが、この機能が見つかりませんでした。Unicode(UTF-8)コードポイントをバイトに変換する

具体的には:UnicodeポイントはU + ########で表されます。これは簡単に取得できます。必要なのは、文字がファイルに書き込まれる形式です(例)。 Unicodeコードポイントは、右端のバイトの7ビットが最初のバイトに書き込まれ、次のビットの6ビットが次のバイトに書き込まれるようにバイトに変換されます。 Emacsは確かにそれを行う方法を知っていますが、UTF-8でエンコードされた文字列のバイトシーケンスをバイトシーケンス(それぞれ8ビットを含む)として取得する方法はありません。

get-byteまたはmultybite-char-to-unibyteなどの機能は、8ビットを超えて使用できない文字でのみ機能します。 get-byteと同じものが必要ですが、マルチバイト文字の場合は、整数0..256の代わりに整数0..256のベクトルまたは1つの長い整数0..2^32を受け取ります。

EDIT

念のために誰もが後で必要になります。

(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)))))))) 

答えて

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) 
関連する問題