简体   繁体   中英

python, from json object (hex) to raw internal binary string

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!

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

also format(..., 'b') to conver to binary (without the 0b prefix)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM