某些十六进制数在写入文件时会被修改



我正在编写一个程序,它将十六进制字符串打包成字节并写入磁盘。我希望文件的十六进制转储与十六进制内容相同。我在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

请帮我解决这个问题。

谢谢!:(

正如您所怀疑的,问题出在编码上。我猜当你在test-write(apply str)时问题就出现了。因此,我将您的代码稍微改写如下:

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
0000040

相关内容

  • 没有找到相关文章

最新更新