Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a padded string to an integer while preserving padding?

I followed the great example at Python: Nicest way to pad zeroes to string (4) but now I need to turn that padded string to a padded integer.

I tried:

list_padded=['0001101', '1100101', '0011011', '0011011', '1101111',
             '0000001', '1110111', '1101111', '0111001', '0011011',
             '0011001'] # My padded sting list. 
    
int_list=[int(x) for x in list_padded] # convert the string to an INT

But what I get is a list of integers sans the padding.

Appreciate any direction or suggestions.

Edit:
After learning the revelation that integers don't get padded, I'm thinking a little differently, however it would probably be a good idea to explain more:

I'm working through a basic encryption exercise in a book. It has given me a list of pseudocode to work through;

  • get cipher string 1-127 and a message, convert both to binary, strip off the 0b, and pad with zeroes.

However it wants me to do the rest WITHOUT XOR! I've gotten that far one line at a time, but now comes this (where the problem begins):

  • Perform manual XOR operation & append binary 7-bit result to encrypted string
  • Convert each binary bit of message character and key to an integer
  • Perform XOR operation on these two bits
  • Convert literal True and False to binary bit & append to output

I'd love to use the XOR operation but am afraid that doing so would mean I'm not going to learn what I need to.

like image 706
Jack Tranner Avatar asked Apr 27 '26 04:04

Jack Tranner


1 Answers

Applying idea of padding to integers is meaningless. If you want to print/represent them you need strings, integers just don't have padding.

like image 160
SilentGhost Avatar answered Apr 28 '26 18:04

SilentGhost



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!