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!

Community
  • 1
  • 1
UnableToLoad
  • 315
  • 6
  • 18

1 Answers1

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

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

JBernardo
  • 32,262
  • 10
  • 90
  • 115