473,381 Members | 1,543 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,381 software developers and data experts.

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

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:
>>import binascii
f1 = 'c:\\temp\\allhex.txt'
f2 = 'c:\\temp\\allbin.txt'
sf = open(f1, 'rU')
df = open(f2, 'w')
slines = sf.readlines()
for line in slines:
.... x = line.rstrip('\n')
.... y = binascii.unhexlify(x)
.... df.write(y)
....
>>df.close()
sf.close()
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:
>>x
'0164'
>>y
'\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

May 30 '07 #1
6 5188
Vishal wrote:
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:
>>>import binascii
f1 = 'c:\\temp\\allhex.txt'
f2 = 'c:\\temp\\allbin.txt'
sf = open(f1, 'rU')
df = open(f2, 'w')
slines = sf.readlines()
for line in slines:
... x = line.rstrip('\n')
... y = binascii.unhexlify(x)
... df.write(y)
...
>>>df.close()
sf.close()
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:
>>>x
'0164'
>>>y
'\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

May 30 '07 #2
On May 30, 1:31 pm, Peter Otten <__pete...@web.dewrote:
Vishal wrote:
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:
>>import binascii
f1 = 'c:\\temp\\allhex.txt'
f2 = 'c:\\temp\\allbin.txt'
sf = open(f1, 'rU')
df = open(f2, 'w')
slines = sf.readlines()
for line in slines:
... x = line.rstrip('\n')
... y = binascii.unhexlify(x)
... df.write(y)
...
>>df.close()
sf.close()

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:
>>x
'0164'
>>y
'\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
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

Jul 11 '07 #3
On Jul 11, 3:21 am, Vishal <vsapr...@gmail.comwrote:
On May 30, 1:31 pm, Peter Otten <__pete...@web.dewrote:


Vishal wrote:
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:
>>>import binascii
>>>f1 = 'c:\\temp\\allhex.txt'
>>>f2 = 'c:\\temp\\allbin.txt'
>>>sf = open(f1, 'rU')
>>>df = open(f2, 'w')
>>>slines = sf.readlines()
>>>for line in slines:
... x = line.rstrip('\n')
... y = binascii.unhexlify(x)
... df.write(y)
...
>>>df.close()
>>>sf.close()
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:
>>>x
'0164'
>>>y
'\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

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:
>>import gmpy
x = 0x0164
s = gmpy.digits(x,2) # convert to base 2
y = '0'*(16-len(s)) + s # pad to 16 bits
y
'0000000101100100'

>
Thanks and best regards,
Vishal
Jul 11 '07 #4
On Wed, 11 Jul 2007 10:46:23 -0700, me********@aol.com wrote:
You can with gmpy:
>>>import gmpy
x = 0x0164
s = gmpy.digits(x,2) # convert to base 2
y = '0'*(16-len(s)) + s # pad to 16 bits
y
'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
Jul 11 '07 #5
On Jul 11, 1:38 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
On Wed, 11 Jul 2007 10:46:23 -0700, mensana...@aol.com wrote:
You can with gmpy:
>>import gmpy
x = 0x0164
s = gmpy.digits(x,2) # convert to base 2
y = '0'*(16-len(s)) + s # pad to 16 bits
y
'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.

Jul 11 '07 #6
En Wed, 11 Jul 2007 17:34:04 -0300, me********@aol.com
<me********@aol.comescribió:
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...

--
Gabriel Genellina

Jul 12 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: nobody | last post by:
1) Does anyone know if the CRC32 algorithm in binascii has a name? There seem to be a TON of different CRC32 methods; different polynomials, different byte orders, different seeds, some flip the...
3
by: Newgene | last post by:
Hi, group, May I ask a newbie question? Given the below example: for i in range(100): print "%s%%" % i I want to show the progress by print 1% to 100%. But I hope each output will clear...
12
by: Larry Bates | last post by:
I'm trying to get the results of binascii.crc32 to match the results of another utility that produces 32 bit unsigned CRCs. binascii.crc32 returns results in the range of -2**31-1 and 2**21-1....
1
by: Jeremy | last post by:
I'm working on a project to create a keyfinder program that finds the Windows CD Key in the registry and decodes it. I prototyped it in Python and it worked great but for several reasons I've...
2
by: Ed Swarthout | last post by:
Why is there no binascii.a2b_binary(bitstr) which returns the binary data represented by the bit string? Like: >>> binascii.a2b_binary('0011001100110101') '35' perl has pack("B*",...
0
by: yaipa | last post by:
I snipped this bit of code out of Andrew Kuchling 'pyCrypto' test fixture. Having a need to XOR Binascii Hex strings in my current project, I found it very helpful to cut down on a bit of code...
9
by: Jess | last post by:
Hello, I tried to clear a vector "v" using "v.clear()". If "v" contains those objects that are non-built-in (e.g. string), then "clear()" can indeed remove all contents. However, if "v"...
2
by: masayuki.takagi | last post by:
hi all, i want to measure memory usage of my python process on Mac OSX. i tired resource module, but it doesn't work on OSX. how can i get it ? thnx.
4
by: Dinsdale | last post by:
I am writing a small console application that runs fine unless I am re- directing the output to a file (i.e. c:\ app.exe >>output.txt) . I have determined that the issue is caused by the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.