473,657 Members | 2,921 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

binascii.unhexl ify ... 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\\allh ex.txt'
f2 = 'c:\\temp\\allb in.txt'
sf = open(f1, 'rU')
df = open(f2, 'w')
slines = sf.readlines()
for line in slines:
.... x = line.rstrip('\n ')
.... y = binascii.unhexl ify(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 5212
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\\allh ex.txt'
f2 = 'c:\\temp\\allb in.txt'
sf = open(f1, 'rU')
df = open(f2, 'w')
slines = sf.readlines()
for line in slines:
... x = line.rstrip('\n ')
... y = binascii.unhexl ify(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\\allh ex.txt'
f2 = 'c:\\temp\\allb in.txt'
sf = open(f1, 'rU')
df = open(f2, 'w')
slines = sf.readlines()
for line in slines:
... x = line.rstrip('\n ')
... y = binascii.unhexl ify(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: 000000010110010 0
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.unhexl ify()???

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\\allh ex.txt'
>>>f2 = 'c:\\temp\\allb in.txt'
>>>sf = open(f1, 'rU')
>>>df = open(f2, 'w')
>>>slines = sf.readlines()
>>>for line in slines:
... x = line.rstrip('\n ')
... y = binascii.unhexl ify(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: 000000010110010 0
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.unhexl ify()???
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
'00000001011001 00'

>
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
'00000001011001 00'
For the padding I'd use the `zfill()` method.

In [15]: a = 0x0164

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

Ciao,
Marc 'BlackJack' Rintsch
Jul 11 '07 #5
On Jul 11, 1:38 pm, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
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
'00000001011001 00'

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

In [15]: a = 0x0164

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

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
3419
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 bits and some XOR the result with something else. I've been looking around and a lot of documents (RFCs, etc) refer to ISO3309 and/or ITU-T V.42 (neither of which appear to be available without paid access), yet are often incompatible with each...
3
13921
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 the previous output before print, so that I can see a
12
9830
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. Has anyone ever worked out any "bit twiddling" code to get a proper unsigned 32 bit result from binascii.crc32? Output snip from test on three files: binascii.crc32=-1412119273, oldcrc32= 2221277246
1
2267
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 decided to rewrite it in C++. I use the module binascii extensively in the Python version but I can't find an equivalent module in C++. I'm not a professional developer so maybe I'm overlooking something simple. In particular I'm trying to find...
2
1858
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*", "0011001100110101"); What is the python way to do this?
0
2304
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 clutter. So if you have a need for a Crypto module, this one seems to work off the self without much effort and it comes /w a nice XOR function too boot. :-) PyCrypto can be found at: http://www.amk.ca/python/code/crypto
9
3749
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" contains built-in types (e.g. int), then "clear()" doesn't remove anything at all. Why does "clear()" have this behaviour? Also, when I copy one vector "v1" from another vector "v2", with "v1" longer than "v2" (e.g. "v1" has 2 elements and "v2" has...
2
4011
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
6263
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 Console.Clear() function. When piping to a file, the console.clear causes the following error: Unhandled Exception: System.IO.IOException: The handle is invalid. at System.IO.IO__Error.WinIOError(Int32 errorCode, String maybeFullPath)
0
8385
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8821
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8723
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8502
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8602
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1941
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.