I have strings of eight 1's and 0's (e.g. 11100011) and I need to take each one and convert them to a single unique byte (or a character, object etc that has a file size of one byte per string when saved to file). And they must be unique per arrangement of 1's and 0's so I can reverse the process later to get each string back.
I'll then take all the bytes (or objects) and save them to file (which I've been using pickle to do so far in experimentation), so that critically the final file size in bytes is the same as the number of 8 character strings I have.
How would I convert the strings, and how would I reverse the process?
Thanks in advance, very much indeed!
Edit: I was asked to provide some examples of my input data. Basically, given an input file of text e.g. 'Hello World' the first part of my code runs an algorithm and outputs a list of 1's and 0's ints:
>>> array = runAlgorithm()
>>> array
[1, 0, 1, 0, ....]
And there are anything from a single to typically thousands of entries in the list. After padding out the list so its number of entries is divisible by 8, I then take the entries, cast each as a string, and concatenate them all together (so technically I do have access to them as 1's and 0's ints). I currently then slice the mega string into many 8-character strings.
I'm unfortunately very new to python and have been searching hard for how I would do this, but always run into a problem.
I first tried using int( '1's and 0's string', 2) on each to get unique int's between 0-255. But then I found (since I'm very much a beginner) that most integers aren't each a byte in size.
Then I tried casting each int as a string, since a regular single string character saves as a byte in size. But then of course for all int's with more than one digit, I had strings two and three bytes in size.
Edit:
My last ditch attempt would be to create my own dictionary for each of the 256 combos of 8-character strings, matching a single character to each string - e.g. {a:00000000, b:00000001, ..... !:10100101,[:010101101,.....} saving each string character to file, and hoping that each character is 1 byte in size.
Would this method work, and are there at least 256 characters that weigh 1 byte each in size?