Bottom Page

Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 HELP: String of Zero's and One's to binary byte
#1
I have some code that I am using to convert a file to bytes then those bytes to strings of 0's and 1's.

with open(file, mode='rb') as file:
content = file.read()
b = bytearray(content)

for number in b:
   data = bin(number)[2:].zfill(8)
   print data
I get output like 10110100, 00000001, etc, etc as it should be.
I want to take those strings of bits I get in my output and convert them back to bytes, then write those bytes back to a new file.
I have tried with the following code:

list = ('11111111', '11111110', '01101000', '00000000', '01101001', '00000000', '00001101', '00000000', '00001010', '00000000')
def writer(stuff):
   blob = struct.pack('!H', int(stuff, 2))
   with open('test.txt', "a+b") as file:
           file.write(blob)
for strings in list:
   writer(strings)
The original text file contains the word "hi" and that is all, but when I write the new file the output is "▒▒hi"
Kebap wrote Apr-07-2017, 12:27 PM:
You can use either [python] or [code] tags but don't need both at the same time :)
Quote
#2
Your code is OK except "B" for "!H".
And you need an editor which is able to open
the file as UTF-16 with BOM.

#!/usr/bin/python3
import struct

lst = ('11111111', '11111110',
       '01101000', '00000000',
       '01101001', '00000000',
       '00001101', '00000000',
       '00001010', '00000000')

def writer(stuff):
   blob = struct.pack('B', int(stuff, 2))
   with open('test.txt', "a+b") as file:
       file.write(blob)

for strings in lst:
   writer(strings)
#done

Or without struct:

#!/usr/bin/python3
lst = ('11111111', '11111110',
       '01101000', '00000000',
       '01101001', '00000000',
       '00001101', '00000000',
       '00001010', '00000000')

def writer(stuff):
   blob = int(stuff, 2).to_bytes(1, byteorder='big')
   with open('test.txt', "a+b") as file:
       file.write(blob)

for strings in lst:
   writer(strings)
#done

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  First Byte of a string is missing while receiving data over TCP Socket shahrukh1987 2 61 3 hours ago
Last Post: DeaD_EyE
  'utf-8' codec can't decode byte 0xda in position 184: invalid continuation byte karkas 7 1,027 Sep-12-2019, 11:19 PM
Last Post: newbieAuggie2019
  Byte string catenation inefficient in 3.7? RMJFlack 13 588 Aug-18-2019, 05:19 AM
Last Post: RMJFlack
  CSV file from Binary to String mr_byte31 2 661 Jul-27-2019, 08:46 PM
Last Post: snippsat
  binary search string help kietrichards 1 298 Mar-08-2019, 12:43 PM
Last Post: stullis
  converting binary b'0x31303032\n' to "1002" string amygdalas 2 454 Nov-07-2018, 03:50 AM
Last Post: amygdalas
  4 byte hex byte swap from binary file medievil 7 7,595 May-08-2018, 08:16 AM
Last Post: killerrex
  get the content of the byte as string ricardons 5 922 Mar-02-2018, 02:41 PM
Last Post: ricardons
  byte string Skaperen 5 1,133 Feb-04-2018, 08:58 AM
Last Post: Gribouillis
  byte string in python2 Skaperen 4 1,555 Nov-23-2017, 03:13 AM
Last Post: Skaperen

Forum Jump:


Users browsing this thread: 1 Guest(s)