あなたの実装(s)はあなたがのバイトをシリアル化する必要が
(defn hexify [s]
(apply str
(map #(format "%02x" (int %)) s)))
(defn unhexify [hex]
(apply str
(map
(fn [[x y]] (char (Integer/parseInt (str x y) 16)))
(partition 2 hex))))
(= "\u2195" (unhexify(hexify "\u2195")))
false ; should be true
はこれを克服するために、非ASCII文字には対応していません文字列は必要な文字エンコーディングを使用します。これは、1文字あたりマルチバイトにすることができます。
これにはいくつかの「問題」があります。
- すべての数値型は、JVMで署名されています。
- 符号なしバイトはありません。
慣用的なJavaでは、整数の下位バイトを使用し、それを使用したときはいつでもこのようにマスクします。
int intValue = 0x80;
byte byteValue = (byte)(intValue & 0xff); -- use only low byte
System.out.println("int:\t" + intValue);
System.out.println("byte:\t" + byteValue);
-- output:
-- int: 128
-- byte: -128
クロージャーは、効果的にこれを行うために(unchecked-byte)
を持っています。あなたがこれを行うことができUTF-8を使用して例えば
、:すべての投稿のソリューションは、いくつかの欠点を持っているので
(defn hexify [s]
(apply str (map #(format "%02x" %) (.getBytes s "UTF-8"))))
(defn unhexify [s]
(let [bytes (into-array Byte/TYPE
(map (fn [[x y]]
(unchecked-byte (Integer/parseInt (str x y) 16)))
(partition 2 s)))]
(String. bytes "UTF-8")))
; with the above implementation:
;=> (hexify "\u2195")
"e28695"
;=> (unhexify "e28695")
"↕"
;=> (= "\u2195" (unhexify (hexify "\u2195")))
true
Javaライブラリを使用しますか? – Marcin
あなたはすでにそれを持っていた – Ankur
@Ankur:明らかにsw1nnの回答として表示されていない - これは私が可能な場合は既存の機能が欲しい理由です。 –