如何将字符串转换为二进制 [英] How to Convert a string into binary
问题描述
Hello All
我是python语言的新手。我正在开发一个gnuradio项目,其中
使用python作为主要的编程语言。我试图将
a消息,文本或数字转换为二进制代码,以便我可以处理它。
i google多次并尝试了很多答案在那里,没有运气所以
远。
是否有办法将字符串转换为
的二进制表示形式ascii table。??
我想要的一个例子是:
hi - > 0110100001101001
谢谢
-HNT
Em S ?? b,2006-04 -15?* s 19:25 +0000,HNT20 escreveu:有没有办法将字符串转换为
ascii表的二进制表示。??
我很好奇......为什么?
不,我没有答案。
-
Felipe。
HNT20写道:Hello All
def ascii_to_bin(char):
ascii = ord(char)
bin = []
while(ascii> 0):
if(ascii& 1)== 1:
bin.append(" 1")
else:
bin.append(" 0")
ascii = ascii>> 1
bin.reverse()
binary ="" .join(bin)
zerofix =(8 - len(二进制))*''0''
返回zerofix +二进制
some_string =''时间到了,拉米? '
binary = []
for some_string中的char:
binary.append(ascii_to_bin(char))
打印二进制文件
print" .join(二进制)
some_string =''时间到了,拉米?''
binary = []
for some_string中的char:
binary.append(ascii_to_bin(char))
打印二进制文件
打印 " .join(binary)
"""
[''01010100'',''01101001'',''01101101' ',''01100101'',''00100000'',
''01110100'',''01101111'',''00100000'',''01100111'',''01101111' ',''00100000'',
''01101110'',''01101111'',''01110111'',''00101100'',''00100000'',''01010010' ',
''01110101'',''01101101'',''01101101'',''01111001'',''00111111'']
01010100 01101001 01101101 01100101 00100000 01110100 01101111 00100000
01100111 01101111 00100000 01101110 01101111 01110111 00101100 00100000
01010010 01110101 01101101 01101101 01111001 00111111
"""
HNT20写道:
有没有办法将字符串转换为二进制表示
ascii表的含义。??
我想要的一个例子是:
hi - > 0110100001101001
为什么不写一些代码来做呢?例如
def b1(n):
return" 01" [n%2]
def b2(n):
返回b1(n>> 1)+ b1(n)
def b3(n):
返回b2(n>>> 2)+ b2(n)
def b4(n):
返回b3(n>> 4)+ b3(n)
bytes = [b4(n)for n in range(256)]
def binstring(s):
return''''。join(bytes [ord(c)] for c in s)
binstring(''hi'')
''0110100001101001''
Hello All
i am new to python language. i am working on a gnuradio project where it
uses python as the primary programming language. i am trying to convert
a message, text, or numbers into binary code so that i can process it.
i googled many times and tried many of the answers out there, no luck so
far.
is there a way to convert a string into its binary representation of the
ascii table.??
an example of what i want would be:
hi --> 0110100001101001
Thanks
-HNT
Em S??b, 2006-04-15 ?*s 19:25 +0000, HNT20 escreveu:is there a way to convert a string into its binary representation of the
ascii table.??
I''m very curious... why?
And no, I don''t have the answer.
--
Felipe.
HNT20 wrote:Hello All
def ascii_to_bin(char):
ascii = ord(char)
bin = []
while (ascii > 0):
if (ascii & 1) == 1:
bin.append("1")
else:
bin.append("0")
ascii = ascii >> 1
bin.reverse()
binary = "".join(bin)
zerofix = (8 - len(binary)) * ''0''
return zerofix + binary
some_string = ''Time to go now, Rummy?''
binary = []
for char in some_string:
binary.append(ascii_to_bin(char))
print binary
print " ".join(binary)
some_string = ''Time to go now, Rummy?''
binary = []
for char in some_string:
binary.append(ascii_to_bin(char))
print binary
print " ".join(binary)
"""
[''01010100'', ''01101001'', ''01101101'', ''01100101'', ''00100000'',
''01110100'', ''01101111'', ''00100000'', ''01100111'', ''01101111'', ''00100000'',
''01101110'', ''01101111'', ''01110111'', ''00101100'', ''00100000'', ''01010010'',
''01110101'', ''01101101'', ''01101101'', ''01111001'', ''00111111'']
01010100 01101001 01101101 01100101 00100000 01110100 01101111 00100000
01100111 01101111 00100000 01101110 01101111 01110111 00101100 00100000
01010010 01110101 01101101 01101101 01111001 00111111
"""
HNT20 wrote:
is there a way to convert a string into its binary representation of the
ascii table.??
an example of what i want would be:
hi --> 0110100001101001
Why not just write some code to do it? e.g.
def b1(n): return "01"[n%2]
def b2(n): return b1(n>>1)+b1(n)
def b3(n): return b2(n>>2)+b2(n)
def b4(n): return b3(n>>4)+b3(n)
bytes = [ b4(n) for n in range(256)]
def binstring(s): return ''''.join(bytes[ord(c)] for c in s)
binstring(''hi'')
''0110100001101001''
这篇关于如何将字符串转换为二进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!