0

in python how can i convert a "json-loaded" object value into raw binary string? ie "0A" to be converted to "1010"?

what i do is the following: read a line from a file, ie assume the file contains this line:

{"hex":"0A01145af1ab"}

i read it with then i load it with json library //ok so far

data = json.loads(a_line)

then i can use data["hex"],

but i need ie. "0A" to be converted to "1010", and i don't know how to do that i read this topic which is similar to my problem, but it didn't help me (base64.b16decode(data["hex"]) returns error)

thanks a lot!

4

1 回答 1

2
>>> bin(int(data['hex'][:2], 16))[2:]
'1010'

format(..., 'b')可以转换为二进制(不带0b前缀)

于 2012-05-25T16:25:20.327 回答