Creating a list of bits?
up vote
2
down vote
favorite
Currently, I have two functions: char2bin
and segmentString
.
segmentString takes a string and a fill character and returns lists of 8 character strings. For example, if there is a 13 character string, it splits it into a list of two strings where the second string has 3 fill characters to make it a complete 8.
>>>segmentString("Hello, World!", "-")
['Hello, W', 'orld!---']
char2bin takes individual string characters (single character) and turns them into a list of 8 bits. It does not work for multiple character strings. For example,
>>>char2bin('a')
[0,1,1,0,0,0,0,1]
>>>char2bin('abc')
(ERROR)
I need to create a function (in this example, let's call it framer) that takes the result from segmentString and convert it into a list of bits, where each list of bits are contained in a separate list within a list.
For example, from the segmentString function, this would create a list of two strings. Each letter of each separate string is converted into a list of bits, and each list of bits is contained as a list for each string.
>>>F=framer("Hello, World!", "-")
>>>F
[[[0, 1, 0, 0, 1, 0, 0, 0], [0, 1, 1, 0, 0, 1, 0, 1], [0,1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 1, 1,1,1], [0, 0, 1, 0, 1, 1, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 1,1, 1,0, 1, 1, 1]], [[0, 1, 1, 0, 1, 1, 1, 1], [0, 1, 1, 1, 0, 0,1, 0], [0,1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 0, 1, 0, 0], [0, 0,1, 0, 0, 0, 0,1], [0, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1,1, 0], [0, 1, 1, 1,1, 1, 1, 0]]]
As you can see, there is one general list that contains two lists that contain 8 lists of bits, which were converted from a string character by char2bin.
How would I do this?
python list bit
New contributor
add a comment |
up vote
2
down vote
favorite
Currently, I have two functions: char2bin
and segmentString
.
segmentString takes a string and a fill character and returns lists of 8 character strings. For example, if there is a 13 character string, it splits it into a list of two strings where the second string has 3 fill characters to make it a complete 8.
>>>segmentString("Hello, World!", "-")
['Hello, W', 'orld!---']
char2bin takes individual string characters (single character) and turns them into a list of 8 bits. It does not work for multiple character strings. For example,
>>>char2bin('a')
[0,1,1,0,0,0,0,1]
>>>char2bin('abc')
(ERROR)
I need to create a function (in this example, let's call it framer) that takes the result from segmentString and convert it into a list of bits, where each list of bits are contained in a separate list within a list.
For example, from the segmentString function, this would create a list of two strings. Each letter of each separate string is converted into a list of bits, and each list of bits is contained as a list for each string.
>>>F=framer("Hello, World!", "-")
>>>F
[[[0, 1, 0, 0, 1, 0, 0, 0], [0, 1, 1, 0, 0, 1, 0, 1], [0,1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 1, 1,1,1], [0, 0, 1, 0, 1, 1, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 1,1, 1,0, 1, 1, 1]], [[0, 1, 1, 0, 1, 1, 1, 1], [0, 1, 1, 1, 0, 0,1, 0], [0,1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 0, 1, 0, 0], [0, 0,1, 0, 0, 0, 0,1], [0, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1,1, 0], [0, 1, 1, 1,1, 1, 1, 0]]]
As you can see, there is one general list that contains two lists that contain 8 lists of bits, which were converted from a string character by char2bin.
How would I do this?
python list bit
New contributor
I would use list comprehension
– YoYoYonnY
Nov 10 at 14:51
1
something likeframer = lambda s, d: [[char2bin(c) for c in seg] for seg in segmentString(s,d)]
– YoYoYonnY
Nov 10 at 15:07
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Currently, I have two functions: char2bin
and segmentString
.
segmentString takes a string and a fill character and returns lists of 8 character strings. For example, if there is a 13 character string, it splits it into a list of two strings where the second string has 3 fill characters to make it a complete 8.
>>>segmentString("Hello, World!", "-")
['Hello, W', 'orld!---']
char2bin takes individual string characters (single character) and turns them into a list of 8 bits. It does not work for multiple character strings. For example,
>>>char2bin('a')
[0,1,1,0,0,0,0,1]
>>>char2bin('abc')
(ERROR)
I need to create a function (in this example, let's call it framer) that takes the result from segmentString and convert it into a list of bits, where each list of bits are contained in a separate list within a list.
For example, from the segmentString function, this would create a list of two strings. Each letter of each separate string is converted into a list of bits, and each list of bits is contained as a list for each string.
>>>F=framer("Hello, World!", "-")
>>>F
[[[0, 1, 0, 0, 1, 0, 0, 0], [0, 1, 1, 0, 0, 1, 0, 1], [0,1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 1, 1,1,1], [0, 0, 1, 0, 1, 1, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 1,1, 1,0, 1, 1, 1]], [[0, 1, 1, 0, 1, 1, 1, 1], [0, 1, 1, 1, 0, 0,1, 0], [0,1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 0, 1, 0, 0], [0, 0,1, 0, 0, 0, 0,1], [0, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1,1, 0], [0, 1, 1, 1,1, 1, 1, 0]]]
As you can see, there is one general list that contains two lists that contain 8 lists of bits, which were converted from a string character by char2bin.
How would I do this?
python list bit
New contributor
Currently, I have two functions: char2bin
and segmentString
.
segmentString takes a string and a fill character and returns lists of 8 character strings. For example, if there is a 13 character string, it splits it into a list of two strings where the second string has 3 fill characters to make it a complete 8.
>>>segmentString("Hello, World!", "-")
['Hello, W', 'orld!---']
char2bin takes individual string characters (single character) and turns them into a list of 8 bits. It does not work for multiple character strings. For example,
>>>char2bin('a')
[0,1,1,0,0,0,0,1]
>>>char2bin('abc')
(ERROR)
I need to create a function (in this example, let's call it framer) that takes the result from segmentString and convert it into a list of bits, where each list of bits are contained in a separate list within a list.
For example, from the segmentString function, this would create a list of two strings. Each letter of each separate string is converted into a list of bits, and each list of bits is contained as a list for each string.
>>>F=framer("Hello, World!", "-")
>>>F
[[[0, 1, 0, 0, 1, 0, 0, 0], [0, 1, 1, 0, 0, 1, 0, 1], [0,1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 1, 1,1,1], [0, 0, 1, 0, 1, 1, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 1,1, 1,0, 1, 1, 1]], [[0, 1, 1, 0, 1, 1, 1, 1], [0, 1, 1, 1, 0, 0,1, 0], [0,1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 0, 1, 0, 0], [0, 0,1, 0, 0, 0, 0,1], [0, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1,1, 0], [0, 1, 1, 1,1, 1, 1, 0]]]
As you can see, there is one general list that contains two lists that contain 8 lists of bits, which were converted from a string character by char2bin.
How would I do this?
python list bit
python list bit
New contributor
New contributor
edited Nov 10 at 14:51
New contributor
asked Nov 10 at 14:47
hello
314
314
New contributor
New contributor
I would use list comprehension
– YoYoYonnY
Nov 10 at 14:51
1
something likeframer = lambda s, d: [[char2bin(c) for c in seg] for seg in segmentString(s,d)]
– YoYoYonnY
Nov 10 at 15:07
add a comment |
I would use list comprehension
– YoYoYonnY
Nov 10 at 14:51
1
something likeframer = lambda s, d: [[char2bin(c) for c in seg] for seg in segmentString(s,d)]
– YoYoYonnY
Nov 10 at 15:07
I would use list comprehension
– YoYoYonnY
Nov 10 at 14:51
I would use list comprehension
– YoYoYonnY
Nov 10 at 14:51
1
1
something like
framer = lambda s, d: [[char2bin(c) for c in seg] for seg in segmentString(s,d)]
– YoYoYonnY
Nov 10 at 15:07
something like
framer = lambda s, d: [[char2bin(c) for c in seg] for seg in segmentString(s,d)]
– YoYoYonnY
Nov 10 at 15:07
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
You can use a list comprehension for this:
def char2bin(byte):
return list(map(int, format(byte, '08b')))
def segmentString(text, padding, chunksize):
for index in range(0, len(text), chunksize):
yield text[index:index + chunksize].ljust(chunksize, padding)
def framer(text, padding='-', chunksize=8, encoding='utf8'):
return [[char2bin(byte) for byte in segment] for segment in
segmentString(text.encode(encoding), padding.encode(encoding), chunksize)]
This uses utf8 encoding, but since your input text is all ascii characters, there's one byte per character.
>>> framer('Hello, World!')
[[[0, 1, 0, 0, 1, 0, 0, 0],
[0, 1, 1, 0, 0, 1, 0, 1],
[0, 1, 1, 0, 1, 1, 0, 0],
[0, 1, 1, 0, 1, 1, 0, 0],
[0, 1, 1, 0, 1, 1, 1, 1],
[0, 0, 1, 0, 1, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 0, 1, 1, 1]],
[[0, 1, 1, 0, 1, 1, 1, 1],
[0, 1, 1, 1, 0, 0, 1, 0],
[0, 1, 1, 0, 1, 1, 0, 0],
[0, 1, 1, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 1],
[0, 0, 1, 0, 1, 1, 0, 1],
[0, 0, 1, 0, 1, 1, 0, 1],
[0, 0, 1, 0, 1, 1, 0, 1]]]
Non-ascii characters require multiple bits to encode.
>>> framer('💩', padding='x00')
[[[1, 1, 1, 1, 0, 0, 0, 0],
[1, 0, 0, 1, 1, 1, 1, 1],
[1, 0, 0, 1, 0, 0, 1, 0],
[1, 0, 1, 0, 1, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]]]
add a comment |
up vote
0
down vote
You could either use list comprehensions or make use of the itertools
module.
You can learn more about list comprehensions here, and more about itertootls here.
add a comment |
up vote
0
down vote
You can use below code to achieve your goal.
def segment_string(s, fill_by):
l =
while s:
if len(s) < 8:
s = s + (fill_by) * (8 - len(s))
l.append(s[0:8])
s = s[8:]
return l # ['Hello, W', 'orld!---']
def char2bin(ch):
a = bin(ord(ch))[2:]
l = [int(c) for c in a]
if len(l) < 8:
l = ([0] * (8 - len(l))) + l # Adding extra 0s to front (if len(l) < 8)
return l # [0, 1, 0, 0, 1, 0, 0, 0]
def framer(s, fill_by='-'):
segments = segment_string(s, fill_by) # Calling segment_string()
print(segments)
arr =
for segment in segments:
arr2 =
for ch in segment:
arr3 = char2bin(ch); # Calling char2bin()
arr2.append(arr3)
arr.append(arr2)
return arr # final list to be returned
if __name__ == "__main__":
f = framer('Hello, World!', '~')
print(f)
Output »
[[[0, 1, 0, 0, 1, 0, 0, 0], [0, 1, 1, 0, 0, 1, 0, 1], [0, 1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 1, 1, 1, 1], [0, 0, 1, 0, 1, 1, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 1, 0, 1, 0, 1, 1, 1]], [[0, 1, 1, 0, 1, 1, 1, 1], [0, 1, 1, 1, 0, 0, 1, 0], [0, 1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 0, 1, 0, 0], [0, 0, 1, 0, 0, 0, 0, 1], [0, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 0]]]
# >>> bin(126)
# '0b1111110'
# >>>
# >>> chr(126)
# '~'
# >>>
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You can use a list comprehension for this:
def char2bin(byte):
return list(map(int, format(byte, '08b')))
def segmentString(text, padding, chunksize):
for index in range(0, len(text), chunksize):
yield text[index:index + chunksize].ljust(chunksize, padding)
def framer(text, padding='-', chunksize=8, encoding='utf8'):
return [[char2bin(byte) for byte in segment] for segment in
segmentString(text.encode(encoding), padding.encode(encoding), chunksize)]
This uses utf8 encoding, but since your input text is all ascii characters, there's one byte per character.
>>> framer('Hello, World!')
[[[0, 1, 0, 0, 1, 0, 0, 0],
[0, 1, 1, 0, 0, 1, 0, 1],
[0, 1, 1, 0, 1, 1, 0, 0],
[0, 1, 1, 0, 1, 1, 0, 0],
[0, 1, 1, 0, 1, 1, 1, 1],
[0, 0, 1, 0, 1, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 0, 1, 1, 1]],
[[0, 1, 1, 0, 1, 1, 1, 1],
[0, 1, 1, 1, 0, 0, 1, 0],
[0, 1, 1, 0, 1, 1, 0, 0],
[0, 1, 1, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 1],
[0, 0, 1, 0, 1, 1, 0, 1],
[0, 0, 1, 0, 1, 1, 0, 1],
[0, 0, 1, 0, 1, 1, 0, 1]]]
Non-ascii characters require multiple bits to encode.
>>> framer('💩', padding='x00')
[[[1, 1, 1, 1, 0, 0, 0, 0],
[1, 0, 0, 1, 1, 1, 1, 1],
[1, 0, 0, 1, 0, 0, 1, 0],
[1, 0, 1, 0, 1, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]]]
add a comment |
up vote
1
down vote
You can use a list comprehension for this:
def char2bin(byte):
return list(map(int, format(byte, '08b')))
def segmentString(text, padding, chunksize):
for index in range(0, len(text), chunksize):
yield text[index:index + chunksize].ljust(chunksize, padding)
def framer(text, padding='-', chunksize=8, encoding='utf8'):
return [[char2bin(byte) for byte in segment] for segment in
segmentString(text.encode(encoding), padding.encode(encoding), chunksize)]
This uses utf8 encoding, but since your input text is all ascii characters, there's one byte per character.
>>> framer('Hello, World!')
[[[0, 1, 0, 0, 1, 0, 0, 0],
[0, 1, 1, 0, 0, 1, 0, 1],
[0, 1, 1, 0, 1, 1, 0, 0],
[0, 1, 1, 0, 1, 1, 0, 0],
[0, 1, 1, 0, 1, 1, 1, 1],
[0, 0, 1, 0, 1, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 0, 1, 1, 1]],
[[0, 1, 1, 0, 1, 1, 1, 1],
[0, 1, 1, 1, 0, 0, 1, 0],
[0, 1, 1, 0, 1, 1, 0, 0],
[0, 1, 1, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 1],
[0, 0, 1, 0, 1, 1, 0, 1],
[0, 0, 1, 0, 1, 1, 0, 1],
[0, 0, 1, 0, 1, 1, 0, 1]]]
Non-ascii characters require multiple bits to encode.
>>> framer('💩', padding='x00')
[[[1, 1, 1, 1, 0, 0, 0, 0],
[1, 0, 0, 1, 1, 1, 1, 1],
[1, 0, 0, 1, 0, 0, 1, 0],
[1, 0, 1, 0, 1, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]]]
add a comment |
up vote
1
down vote
up vote
1
down vote
You can use a list comprehension for this:
def char2bin(byte):
return list(map(int, format(byte, '08b')))
def segmentString(text, padding, chunksize):
for index in range(0, len(text), chunksize):
yield text[index:index + chunksize].ljust(chunksize, padding)
def framer(text, padding='-', chunksize=8, encoding='utf8'):
return [[char2bin(byte) for byte in segment] for segment in
segmentString(text.encode(encoding), padding.encode(encoding), chunksize)]
This uses utf8 encoding, but since your input text is all ascii characters, there's one byte per character.
>>> framer('Hello, World!')
[[[0, 1, 0, 0, 1, 0, 0, 0],
[0, 1, 1, 0, 0, 1, 0, 1],
[0, 1, 1, 0, 1, 1, 0, 0],
[0, 1, 1, 0, 1, 1, 0, 0],
[0, 1, 1, 0, 1, 1, 1, 1],
[0, 0, 1, 0, 1, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 0, 1, 1, 1]],
[[0, 1, 1, 0, 1, 1, 1, 1],
[0, 1, 1, 1, 0, 0, 1, 0],
[0, 1, 1, 0, 1, 1, 0, 0],
[0, 1, 1, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 1],
[0, 0, 1, 0, 1, 1, 0, 1],
[0, 0, 1, 0, 1, 1, 0, 1],
[0, 0, 1, 0, 1, 1, 0, 1]]]
Non-ascii characters require multiple bits to encode.
>>> framer('💩', padding='x00')
[[[1, 1, 1, 1, 0, 0, 0, 0],
[1, 0, 0, 1, 1, 1, 1, 1],
[1, 0, 0, 1, 0, 0, 1, 0],
[1, 0, 1, 0, 1, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]]]
You can use a list comprehension for this:
def char2bin(byte):
return list(map(int, format(byte, '08b')))
def segmentString(text, padding, chunksize):
for index in range(0, len(text), chunksize):
yield text[index:index + chunksize].ljust(chunksize, padding)
def framer(text, padding='-', chunksize=8, encoding='utf8'):
return [[char2bin(byte) for byte in segment] for segment in
segmentString(text.encode(encoding), padding.encode(encoding), chunksize)]
This uses utf8 encoding, but since your input text is all ascii characters, there's one byte per character.
>>> framer('Hello, World!')
[[[0, 1, 0, 0, 1, 0, 0, 0],
[0, 1, 1, 0, 0, 1, 0, 1],
[0, 1, 1, 0, 1, 1, 0, 0],
[0, 1, 1, 0, 1, 1, 0, 0],
[0, 1, 1, 0, 1, 1, 1, 1],
[0, 0, 1, 0, 1, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 0, 1, 1, 1]],
[[0, 1, 1, 0, 1, 1, 1, 1],
[0, 1, 1, 1, 0, 0, 1, 0],
[0, 1, 1, 0, 1, 1, 0, 0],
[0, 1, 1, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 1],
[0, 0, 1, 0, 1, 1, 0, 1],
[0, 0, 1, 0, 1, 1, 0, 1],
[0, 0, 1, 0, 1, 1, 0, 1]]]
Non-ascii characters require multiple bits to encode.
>>> framer('💩', padding='x00')
[[[1, 1, 1, 1, 0, 0, 0, 0],
[1, 0, 0, 1, 1, 1, 1, 1],
[1, 0, 0, 1, 0, 0, 1, 0],
[1, 0, 1, 0, 1, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]]]
edited Nov 10 at 16:33
answered Nov 10 at 15:46
Håken Lid
10.3k62440
10.3k62440
add a comment |
add a comment |
up vote
0
down vote
You could either use list comprehensions or make use of the itertools
module.
You can learn more about list comprehensions here, and more about itertootls here.
add a comment |
up vote
0
down vote
You could either use list comprehensions or make use of the itertools
module.
You can learn more about list comprehensions here, and more about itertootls here.
add a comment |
up vote
0
down vote
up vote
0
down vote
You could either use list comprehensions or make use of the itertools
module.
You can learn more about list comprehensions here, and more about itertootls here.
You could either use list comprehensions or make use of the itertools
module.
You can learn more about list comprehensions here, and more about itertootls here.
answered Nov 10 at 14:57
user4437749
506
506
add a comment |
add a comment |
up vote
0
down vote
You can use below code to achieve your goal.
def segment_string(s, fill_by):
l =
while s:
if len(s) < 8:
s = s + (fill_by) * (8 - len(s))
l.append(s[0:8])
s = s[8:]
return l # ['Hello, W', 'orld!---']
def char2bin(ch):
a = bin(ord(ch))[2:]
l = [int(c) for c in a]
if len(l) < 8:
l = ([0] * (8 - len(l))) + l # Adding extra 0s to front (if len(l) < 8)
return l # [0, 1, 0, 0, 1, 0, 0, 0]
def framer(s, fill_by='-'):
segments = segment_string(s, fill_by) # Calling segment_string()
print(segments)
arr =
for segment in segments:
arr2 =
for ch in segment:
arr3 = char2bin(ch); # Calling char2bin()
arr2.append(arr3)
arr.append(arr2)
return arr # final list to be returned
if __name__ == "__main__":
f = framer('Hello, World!', '~')
print(f)
Output »
[[[0, 1, 0, 0, 1, 0, 0, 0], [0, 1, 1, 0, 0, 1, 0, 1], [0, 1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 1, 1, 1, 1], [0, 0, 1, 0, 1, 1, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 1, 0, 1, 0, 1, 1, 1]], [[0, 1, 1, 0, 1, 1, 1, 1], [0, 1, 1, 1, 0, 0, 1, 0], [0, 1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 0, 1, 0, 0], [0, 0, 1, 0, 0, 0, 0, 1], [0, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 0]]]
# >>> bin(126)
# '0b1111110'
# >>>
# >>> chr(126)
# '~'
# >>>
add a comment |
up vote
0
down vote
You can use below code to achieve your goal.
def segment_string(s, fill_by):
l =
while s:
if len(s) < 8:
s = s + (fill_by) * (8 - len(s))
l.append(s[0:8])
s = s[8:]
return l # ['Hello, W', 'orld!---']
def char2bin(ch):
a = bin(ord(ch))[2:]
l = [int(c) for c in a]
if len(l) < 8:
l = ([0] * (8 - len(l))) + l # Adding extra 0s to front (if len(l) < 8)
return l # [0, 1, 0, 0, 1, 0, 0, 0]
def framer(s, fill_by='-'):
segments = segment_string(s, fill_by) # Calling segment_string()
print(segments)
arr =
for segment in segments:
arr2 =
for ch in segment:
arr3 = char2bin(ch); # Calling char2bin()
arr2.append(arr3)
arr.append(arr2)
return arr # final list to be returned
if __name__ == "__main__":
f = framer('Hello, World!', '~')
print(f)
Output »
[[[0, 1, 0, 0, 1, 0, 0, 0], [0, 1, 1, 0, 0, 1, 0, 1], [0, 1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 1, 1, 1, 1], [0, 0, 1, 0, 1, 1, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 1, 0, 1, 0, 1, 1, 1]], [[0, 1, 1, 0, 1, 1, 1, 1], [0, 1, 1, 1, 0, 0, 1, 0], [0, 1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 0, 1, 0, 0], [0, 0, 1, 0, 0, 0, 0, 1], [0, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 0]]]
# >>> bin(126)
# '0b1111110'
# >>>
# >>> chr(126)
# '~'
# >>>
add a comment |
up vote
0
down vote
up vote
0
down vote
You can use below code to achieve your goal.
def segment_string(s, fill_by):
l =
while s:
if len(s) < 8:
s = s + (fill_by) * (8 - len(s))
l.append(s[0:8])
s = s[8:]
return l # ['Hello, W', 'orld!---']
def char2bin(ch):
a = bin(ord(ch))[2:]
l = [int(c) for c in a]
if len(l) < 8:
l = ([0] * (8 - len(l))) + l # Adding extra 0s to front (if len(l) < 8)
return l # [0, 1, 0, 0, 1, 0, 0, 0]
def framer(s, fill_by='-'):
segments = segment_string(s, fill_by) # Calling segment_string()
print(segments)
arr =
for segment in segments:
arr2 =
for ch in segment:
arr3 = char2bin(ch); # Calling char2bin()
arr2.append(arr3)
arr.append(arr2)
return arr # final list to be returned
if __name__ == "__main__":
f = framer('Hello, World!', '~')
print(f)
Output »
[[[0, 1, 0, 0, 1, 0, 0, 0], [0, 1, 1, 0, 0, 1, 0, 1], [0, 1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 1, 1, 1, 1], [0, 0, 1, 0, 1, 1, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 1, 0, 1, 0, 1, 1, 1]], [[0, 1, 1, 0, 1, 1, 1, 1], [0, 1, 1, 1, 0, 0, 1, 0], [0, 1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 0, 1, 0, 0], [0, 0, 1, 0, 0, 0, 0, 1], [0, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 0]]]
# >>> bin(126)
# '0b1111110'
# >>>
# >>> chr(126)
# '~'
# >>>
You can use below code to achieve your goal.
def segment_string(s, fill_by):
l =
while s:
if len(s) < 8:
s = s + (fill_by) * (8 - len(s))
l.append(s[0:8])
s = s[8:]
return l # ['Hello, W', 'orld!---']
def char2bin(ch):
a = bin(ord(ch))[2:]
l = [int(c) for c in a]
if len(l) < 8:
l = ([0] * (8 - len(l))) + l # Adding extra 0s to front (if len(l) < 8)
return l # [0, 1, 0, 0, 1, 0, 0, 0]
def framer(s, fill_by='-'):
segments = segment_string(s, fill_by) # Calling segment_string()
print(segments)
arr =
for segment in segments:
arr2 =
for ch in segment:
arr3 = char2bin(ch); # Calling char2bin()
arr2.append(arr3)
arr.append(arr2)
return arr # final list to be returned
if __name__ == "__main__":
f = framer('Hello, World!', '~')
print(f)
Output »
[[[0, 1, 0, 0, 1, 0, 0, 0], [0, 1, 1, 0, 0, 1, 0, 1], [0, 1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 1, 1, 1, 1], [0, 0, 1, 0, 1, 1, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0], [0, 1, 0, 1, 0, 1, 1, 1]], [[0, 1, 1, 0, 1, 1, 1, 1], [0, 1, 1, 1, 0, 0, 1, 0], [0, 1, 1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 0, 1, 0, 0], [0, 0, 1, 0, 0, 0, 0, 1], [0, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 0]]]
# >>> bin(126)
# '0b1111110'
# >>>
# >>> chr(126)
# '~'
# >>>
edited Nov 10 at 15:40
answered Nov 10 at 15:34
hygull
2,69311126
2,69311126
add a comment |
add a comment |
hello is a new contributor. Be nice, and check out our Code of Conduct.
hello is a new contributor. Be nice, and check out our Code of Conduct.
hello is a new contributor. Be nice, and check out our Code of Conduct.
hello is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240083%2fcreating-a-list-of-bits%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
I would use list comprehension
– YoYoYonnY
Nov 10 at 14:51
1
something like
framer = lambda s, d: [[char2bin(c) for c in seg] for seg in segmentString(s,d)]
– YoYoYonnY
Nov 10 at 15:07