473,503 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Ascii to binary conversion

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")
Aug 9 '08 #1
7 10580
On Aug 9, 11:18 pm, azrael <jura.gro...@gmail.comwrote:
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???

Aug 9 '08 #2
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. :)

On 9 kol, 15:39, John Machin <sjmac...@lexicon.netwrote:
On Aug 9, 11:18 pm, azrael <jura.gro...@gmail.comwrote:
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???
Aug 9 '08 #3
azrael wrote:
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. :)

On 9 kol, 15:39, John Machin <sjmac...@lexicon.netwrote:
>On Aug 9, 11:18 pm, azrael <jura.gro...@gmail.comwrote:
>>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
Aug 9 '08 #4
On Aug 9, 8:18�am, azrael <jura.gro...@gmail.comwrote:
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
>>bin(ord('1'))
'0b110001'

But it has that worthless '0b' decorator, but that's
easily dealt with.
>>bin(ord('1'))[2:]
'110001'

And you won't get leading 0s unless you ask for them.
>>bin(ord('1'))[2:].zfill(8)
'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.
>>import gmpy
>>gmpy.digits(ord('1')) # base 10 by default
'49'
>>gmpy.digits(ord('1'),2) # same in base 2 (note - no decorator)
'110001'
>>gmpy.digits(ord('1'),2).zfill(8) # add leading 0s
'00110001'
>>gmpy.popcount(ord('1')) # of 1-bits
3
>>gmpy.scan1(ord('1')) # the first 1 is at bit 0
0
>>gmpy.scan1(ord('1'),1) # the next 1 is at bit 4
4
>>gmpy.hamdist(ord('1'),ord('6')) # differ at 3 bit positions
3

And you probably won't find anything faster than gmpy.
Aug 9 '08 #5
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
On 9 kol, 16:47, Larry Bates <larry.ba...@websafe.com`wrote:
azrael wrote:
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. :)
On 9 kol, 15:39, John Machin <sjmac...@lexicon.netwrote:
On Aug 9, 11:18 pm, azrael <jura.gro...@gmail.comwrote:
>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
Aug 9 '08 #6
On Aug 10, 12:37 am, azrael <jura.gro...@gmail.comwrote:
[top-posting corrected]
>
On 9 kol, 15:39, John Machin <sjmac...@lexicon.netwrote:
On Aug 9, 11:18 pm, azrael <jura.gro...@gmail.comwrote:
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")
>why are you doing this so much that the speed matters???
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?
Aug 9 '08 #7


azrael wrote:
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:
>>0b00111010101
469
>>b='111010101'
eval('0b'+b)
469
>>'{0:b}'.format(469)
'111010101'

Aug 9 '08 #8

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

Similar topics

5
15016
by: Lenard Gunda | last post by:
hi! I have the following problem. I need to read data from a TXT file our company receives. I would use StreamReader, and process it line by line using ReadLine, however, the following problem...
8
14267
by: davihigh | last post by:
My Friends: I am using std::ofstream (as well as ifstream), I hope that when i wrote in some std::string(...) with locale, ofstream can convert to UTF-8 encoding and save file to disk. So does...
13
3530
by: greg | last post by:
Hello, I'm searching to know if a local file is ascii or binary. I couldn't find it in the manual, is there a way to know that ? thanks, -- greg
6
10021
by: SandyMan | last post by:
Hi, I am able to open a binary file for reading but can someone tell me as how to go about converting a Binary file to ASCII file using C. Thanks In Advance SandyMan
10
3825
by: Wrecked | last post by:
Could someone suggest a method to convert a Binary data to an ASCII data, with very less or no increase in the memory . The problem basically is there is an excrypted message which needs to be...
24
2976
by: pbd22 | last post by:
Hi. I want to know the size of a file (prior to FTP) in ASCII Mode. The reason is that I want to know how bit the file is going to be once it reaches the FTP server and there is a size...
1
2584
by: Vic | last post by:
I have a c program which writes mac address entries onto a text file. Text file when opened in vim looks like this index mac 1 ^@^@^Q^@^@^A ascii of (00.00.11.00.00.01) 2 ^@^@^Q^@^@^B...
9
3880
by: =?Utf-8?B?UGhhbmlkaGFy?= | last post by:
Hi, I'm developing a Winform application in C#( .net 2.0). I've a dialog box where user can input text and that text would be sent across to other machine using sockets. When the user enters...
12
3680
by: fermineutron | last post by:
I am trying to write a function which will convert a large ascii number, say 100 ascii digits, to its binary representation. It seems that evey algorithm I am trying to think of is backwards. ...
0
7203
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,...
0
7089
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7282
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,...
0
7339
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...
0
4678
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3157
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1515
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 ...
0
389
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...

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.