我正在编写一个程序,它将十六进制字符串打包成字节并将它们写到磁盘上。我希望文件的十六进制与十六进制一样。我在Clojure做这件事:
(defn- hex-char->int
[hex-char]
(-> hex-char
str
(Integer/parseInt 16)))
(defn- pack
[hex-1 hex-2]
(-> hex-1
(bit-shift-left 4)
(bit-or hex-2)
unchecked-char))
(defn- hex-str->packed-bytes
[hex-str]
(->> hex-str
(map hex-char->int)
(partition 2)
(mapv (partial apply pack))))
(defn write-bytes
[bs]
(with-open [f (io/output-stream "test.txt")]
(.write f (.getBytes bs))))
(defn test-write
[hex-str]
(->> hex-str
hex-str->packed-bytes
(apply str)
write-bytes))这个程序很适用于从"00“到"7f”的六角形夫妇。当我将输出文件转储时,我可以看到相同的十六进制数。
但是对于从"80“到"ff”的字符来说,这是行不通的。用于"80“的十六进制是"c280”,而对于"ff“则是"c3bf”。
如果我不转换为字符并直接用字节写入,就可以解决这个问题,所以我假设这与编码有关。我甚至发现了这个:https://superuser.com/questions/1349494/filling-file-with-0xff-gives-c3bf-in-osx
但我想了解如何在Clojure的上下文中解决这个问题。
粘贴`000f101f202f303f404f505f606f707f808f909fa0afb0bfc0cfd0dfe0eff0ff“的六转储以供参考:
00000000 00 0f 10 1f 20 2f 30 3f 40 4f 50 5f 60 6f 70 7f |.... /0?@OP_`op.|
00000010 c2 80 c2 8f c2 90 c2 9f c2 a0 c2 af c2 b0 c2 bf |................|
00000020 c3 80 c3 8f c3 90 c3 9f c3 a0 c3 af c3 b0 c3 bf |................|
00000030请帮我解决这个问题。
谢谢!:)
发布于 2022-03-30 23:22:36
正如你所怀疑的,问题在于编码。我猜想,当您在(apply str) in test-write时,问题正在发生。因此,我稍微重写了您的代码如下:
user> (defn- hex-char->int
[hex-char]
(-> hex-char
str
(Integer/parseInt 16)))
#'user/hex-char->int
user> (defn- pack
[hex-1 hex-2]
(-> hex-1
(bit-shift-left 4)
(bit-or hex-2)))
#'user/pack
user> (defn- hex-str->packed-bytes
[hex-str]
(->> hex-str
(map hex-char->int)
(partition 2)
(mapv (partial apply pack))))
#'user/hex-str->packed-bytes
user> (defn write-bytes
[bs]
(with-open [f (io/output-stream "test.txt")]
(.write f bs)))
#'user/write-bytes
user> (defn test-write
[hex-str]
(->> hex-str
hex-str->packed-bytes
(mapv unchecked-byte)
(byte-array)
write-bytes))
#'user/test-write
user> (test-write "000f101f202f303f404f505f606f707f808f909fa0afb0bfc0cfd0dfe0eff0ff")
nil
user> 并以十六进制显示结果文件的内容:
dorabs-imac:example dorab$ od -h test.txt
0000000 0f00 1f10 2f20 3f30 4f40 5f50 6f60 7f70
0000020 8f80 9f90 afa0 bfb0 cfc0 dfd0 efe0 fff0
0000040https://stackoverflow.com/questions/71684203
复制相似问题