binascii.unhexlify ... not clear about usage, and output

V

Vishal

Hi,

I have a file with a long list of hex characters, and I want to get a
file with corresponding binary characters

here's what I did:
.... x = line.rstrip('\n')
.... y = binascii.unhexlify(x)
.... df.write(y)
....
But what I get is all garbage, atleast textpad and notepad show that
I tried doing it for only one string, and this is what I am seeing on
the interpreter:
'\x01d'

I was expecting 'y' would come out as a string with binary
characters!!!

What am i missing here? Can someone please help.

Thanks and best regards,
Vishal
 
P

Peter Otten

Vishal said:
I have a file with a long list of hex characters, and I want to get a
file with corresponding binary characters

here's what I did:

... x = line.rstrip('\n')
... y = binascii.unhexlify(x)
... df.write(y)
...

Your code is OK, but you have to open f2 in binary mode if your data is
truly binary (an image, say).
But what I get is all garbage, atleast textpad and notepad show that
I tried doing it for only one string, and this is what I am seeing on
the interpreter:

'\x01d'

I was expecting 'y' would come out as a string with binary
characters!!!

What are "binary characters"?
What am i missing here? Can someone please help.

What /exactly/ did you expect? Note that "\x01d" and "\x01\x64" are just
different renderings of the same string chr(0x01) + chr(0x64).

Peter
 
V

Vishal

Your code is OK, but you have to open f2 in binary mode if your data is
truly binary (an image, say).


What are "binary characters"?


What /exactly/ did you expect? Note that "\x01d" and "\x01\x64" are just
different renderings of the same string chr(0x01) + chr(0x64).

Peter

Thanks Peter for the explanation. Actually what I want is:

if Input is 0x0164, Output should be: 0000000101100100
where each nibble is separated and the output appears in terms of '0's
and '1's.

Can I do that with this function binascii.unhexlify()???

Thanks and best regards,
Vishal
 
M

mensanator

Thanks Peter for the explanation. Actually what I want is:

if Input is 0x0164, Output should be: 0000000101100100
where each nibble is separated and the output appears in terms of '0's
and '1's.

Can I do that with this function binascii.unhexlify()???

You can with gmpy:
 
M

Marc 'BlackJack' Rintsch

You can with gmpy:

'0000000101100100'

For the padding I'd use the `zfill()` method.

In [15]: a = 0x0164

In [16]: gmpy.digits(a, 2).zfill(16)
Out[16]: '0000000101100100'

Ciao,
Marc 'BlackJack' Rintsch
 
M

mensanator

You can with gmpy:
'0000000101100100'

For the padding I'd use the `zfill()` method.

In [15]: a = 0x0164

In [16]: gmpy.digits(a, 2).zfill(16)
Out[16]: '0000000101100100'

Ciao,
Marc 'BlackJack' Rintsch

Damn, I didn't know you could do that.
If only there was a built-in base 2 conversion.
 
G

Gabriel Genellina

En Wed, 11 Jul 2007 17:34:04 -0300, (e-mail address removed)
If only there was a built-in base 2 conversion.

No builtin, but you can find uncountable versions if you search this
group, or the Python cookbook, or the entire web...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top