bin2chr("01110011") # = 15 function ?

G

gcmartijn

H!

I'm searching for a simple
bin2chr("01110011") function that can convert all my 8bit data to a
chr so I can use something like this:

def bin(i):
l = ['0000', '0001', '0010', '0011', '0100', '0101', '0110',
'0111',
'1000', '1001', '1010', '1011', '1100', '1101', '1110',
'1111']
s = ''.join(map(lambda x, l=l: l[int(x, 16)], hex(i)[2:]))
if s[0] == '1' and i > 0:
s = '0000' + s
return s

print ord("s") # = 115
print bin(ord("s")) # = 01110011

test= open('output.ext,'wb')
test.write(bin2chr("01110011"))
test.write(bin2chr("01111011"))
test.write(bin2chr("01110111"))
test.close()

I think this is very easy but I'm not very good in python.
In a other program I use I do something like this

Function bin2int(b$)
blen=Len(b)
For f=1 To blen
n=n Shl 1 + (Mid(b,f,1)="1")
Next
Return n
End Function
 
T

Tim Chase

I'm searching for a simple
bin2chr("01110011") function that can convert all my 8bit data to a
chr so I can use something like this:
print ord("s") # = 115
print bin(ord("s")) # = 01110011

test= open('output.ext,'wb')
test.write(bin2chr("01110011"))
test.write(bin2chr("01111011"))
test.write(bin2chr("01110111"))
test.close()

I think this simple one-liner should do it for you:
's'

which exploits int()'s ability to take a base (2, in this case).

-tkc
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top