Ascii to binary conversion

A

azrael

Hy folks,

I googled, and searched, and can not bealive that I have not found a
built in way to convert the easy and elegant python way a function to
easily convert simple ascii data to binary and back.

I've written some my own but they were pretty slow using binascii
linbrary with hexifly and unhexifly functions conbined with a
lookuptable of binary and hex values.

Any idea how to easily write a function that recieves a character or
string and returns a binary number like:
ascii("1") is converted to bin("00110001")
 
J

John Machin

Hy folks,

I googled, and searched, and can not bealive that I have not found a
built in way to convert the easy and elegant python way a function to
easily convert simple ascii data to binary and back.

I've written some my own but they were pretty slow using binascii
linbrary with hexifly and unhexifly functions conbined with a
lookuptable of binary and hex values.

Any idea how to easily write a function that recieves a character or
string and returns a binary number like:
ascii("1") is converted to bin("00110001")

Here's one way:
.... ai = ord(a)
.... return ''.join('01'[(ai >> x) & 1] for x in xrange(7, -1, -1))
....
BUT ... why are you doing this so much that the speed matters???
 
A

azrael

looks nice. is there an oposite function of ord() so I could also
bring a binary number also back to ascii.

the speed matters if you plan to exchange about 10 M ascii chars and
don't wont to wait a year for the results. :)



Hy folks,
I googled, and searched, and can not bealive that I have not found a
built in way to convert the easy and elegant python way a function to
easily convert simple ascii data to binary and back.
I've written some my own but they were pretty slow using binascii
linbrary with hexifly and unhexifly functions conbined with a
lookuptable of binary and hex values.
Any idea how to easily write a function that recieves a character or
string and returns a binary number like:
ascii("1") is converted to bin("00110001")

Here's one way:

...    ai = ord(a)
...    return ''.join('01'[(ai >> x) & 1] for x in xrange(7, -1, -1))
...
'11111111'

BUT ... why are you doing this so much that the speed matters???
 
M

Mensanator

Hy folks,

I googled, and searched, and can not bealive that I have not found a
built in way to convert the easy and elegant python way a function to
easily convert simple ascii data to binary and back.

I've written some my own but they were pretty slow using binascii
linbrary with hexifly and unhexifly functions conbined with a
lookuptable of binary and hex values.

Any idea how to easily write a function that recieves a character or
string and returns a binary number like:
ascii("1") is converted to bin("00110001")

In Pthon 2.6 & 3.0 there actually IS a bin() function.

IDLE 2.6b1'0b110001'

But it has that worthless '0b' decorator, but that's
easily dealt with.
'110001'

And you won't get leading 0s unless you ask for them.
'00110001'

If you have previous Python versions (and even if you
have 2.6), you might consider getting the gmpy module.
It will convert to any base from 2 to 36 and in addition,
provides a nice set of binary manipulation functions
such as popcount (number of 1-bits), scan1 (finds posistion
of first 1-bit), Hamming Distance (number of differing
bits between two numbers), etc.
'49'
'110001'
'00110001'
3
0
4
3

And you probably won't find anything faster than gmpy.
 
A

azrael

You see, I don't like reading some tutorials. I pick myself a problem
and look for ways to slove it. I am using Python for about 2 years,
but mostly for image processing. As you say, ord is oposite to chr. I
learn by example.

thnx guys, this looks great


azrael said:
looks nice. is there an oposite function of ord() so I could also
bring a binary number also back to ascii.
the speed matters if you plan to exchange about 10 M ascii chars and
don't wont to wait a year for the results. :)
Hy folks,
I googled, and searched, and can not bealive that I have not found a
built in way to convert the easy and elegant python way a function to
easily convert simple ascii data to binary and back.
I've written some my own but they were pretty slow using binascii
linbrary with hexifly and unhexifly functions conbined with a
lookuptable of binary and hex values.
Any idea how to easily write a function that recieves a character or
string and returns a binary number like:
ascii("1") is converted to bin("00110001")
Here's one way:
def a2b(a):
...    ai = ord(a)
...    return ''.join('01'[(ai >> x) & 1] for x in xrange(7, -1, -1))
...
a2b('1')
'00110001'
a2b('2')
'00110010'
a2b(chr(0))
'00000000'
a2b(chr(255))
'11111111'
BUT ... why are you doing this so much that the speed matters???

Opposite of ord() is chr().  These functions have been available in every
language I've used for the last 30 years.  I would suggest that you might want
to spend a little time reading a good Python book and to work through the
tutorial.  It will be worth your investment of time.

-Larry
 
J

John Machin

the speed matters if you plan to exchange about 10 M ascii chars and
don't wont to wait a year for the results. :)

WHY do you plan to change about 10 M ascii chars into about 90 M
bytes? Who or what is going to consume the output?
 
T

Terry Reedy

azrael said:
Any idea how to easily write a function that recieves a character or
string and returns a binary number like:
ascii("1") is converted to bin("00110001")

Other alternatives in 2.6 (I believe) and 3.0:'111010101'
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top