gpt4 book ai didi

haskell - 为什么我的 ByteString 在我的 x86_64 架构上是 BigEndian 格式?

转载 作者:行者123 更新时间:2023-12-05 09:03:44 27 4
gpt4 key购买 nike

这段代码:

import Data.Binary
import qualified Data.ByteString.Internal as BS (c2w, w2c)
import qualified Data.ByteString.Lazy as B
import Data.Int

main :: IO ()
main = do
B.putStr $ encode $ (0x01020304 :: Int32)

在我的机器上产生这个输出(通过 xxd 运行):

00000000: 0102 0304                                ....

这告诉我它是按大端顺序输出的,但是我有一个 x86_64 系统,所以...这是怎么回事?

最佳答案

Data.Binary 模块在 documentation 中指定:

Values encoded using the Binary class are always encoded in network order (big endian) form, and encoded data should be portable across machine endianness, word size, or compiler version. For example, data encoded using the Binary class could be written on any machine, and read back on any another.

因此它不适用于机器的 then endianess,但始终使用 big endian,这样将对象编码为 ByteString,也可以用另一台机器解码。

关于haskell - 为什么我的 ByteString 在我的 x86_64 架构上是 BigEndian 格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69608195/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com