【问题标题】:Convert binary string to hex for curl request将二进制字符串转换为十六进制以进行 curl 请求
【发布时间】:2018-11-20 19:01:29
【问题描述】:

我有一个像

这样的结构包
def encode_number(value):
    return struct.pack('>I', value)


def decode_number(raw):
    return int.from_bytes(raw, byteorder='big') 

>>> encode_number(1) b'\x00\x00\x00\x01'

>>> decode_number(b'\x00\x00\x00\x01') 1

虽然通过 curl 给出的交易是以十六进制给出的,比如 curl http://localhost:26657/broadcast_tx_commit?tx=0x01

所以 0x01 是十六进制的 1,如何轻松地将二进制字符串转换为十六进制以用于 curl 命令? (最好在 linux 终端中使用)

【问题讨论】:

    标签: python curl hex binarystream


    【解决方案1】:

    刚刚谷歌搜索了一下,发现了这个: https://unix.stackexchange.com/questions/65280/binary-to-hexadecimal-and-decimal-in-a-shell-script

    他们用于二进制到十六进制(Bash/Linux 终端)的示例:

    $ printf '%x\n' "$((2#101010101))"
    155
    

    【讨论】:

    • 你是curl http://localhost:26657/broadcast_tx_commit?tx=0x115还是curl http://localhost:26657/broadcast_tx_commit?tx=115?我都累了,而且出现了错误
    • 不确定。我的猜测是这将取决于后端。你可以两个都试试吗? “0x115”对我来说更有意义。
    猜你喜欢
    • 1970-01-01
    • 2019-08-23
    • 1970-01-01
    • 2013-11-24
    • 2021-10-30
    • 2017-05-26
    • 1970-01-01
    • 2017-02-23
    相关资源
    最近更新 更多