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!

2
  • 3
    bin(int(data['hex'][:2], 16)) Commented May 25, 2012 at 16:23
  • @JBernardo -- you get the win. Commented May 25, 2012 at 16:24

1 Answer 1

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

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.