Найти - Пользователи
Полная версия: str в двоичную систему
Начало » Python для новичков » str в двоичную систему
1
sonniy
как можно строку перевести в единицы и нули ?

PS
python ниже 3
s0rg
In [1]: import random

In [2]: a = 'this is a test'

In [3]: b = [random.choice((0,1,)) for c in a]

In [4]: b
Out[4]: [0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0]
pill
Какой вопрос - такой ответ :). По сути строка уже является бинарной, так что вопрос зачем?
Но если просто вывести на экран, то можно например как-то так:

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)
http://www.velocityreviews.com/forums/t356668-how-to-convert-a-string-into-binary.html
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB