3

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.

moken
  • 3,227
  • 8
  • 13
  • 23
  • When you say you want a "padded integer", what do you really need? How are you using such a number? – Karmastan Jul 12 '10 at 21:08
  • Karmastan: I'm working through a basic encryption in a book. Rather than using the XOR operation the exercise is leading me through a series of psuedocode intended to teach me how it all works without XOR. So at this point the pseduocode reads: - Get integer ascii value of each character in message (done) - Get binary value of ascii version of character and strip off "0b" (done) And this is where the problem starts: - Check for 7-character length (pad beginning with zeros if necessary) (done) - Convert each binary bit of message character and key to an integer (problem) Thanks! – Jack Tranner Jul 12 '10 at 21:38
  • 1
    Since your problem has changed so drastically, you should consider opening a new question. – Ignacio Vazquez-Abrams Jul 12 '10 at 22:37

4 Answers4

10

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

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
5

Integers don't have a concept of padding, but if you want then you can store both the value and the original length instead of just the value:

int_list = [(int(x), len(x)) for x in list_padded]

Then if you want to reconstruct the original data you have enough information to do so. You may even want to make a custom class to store both these fields instead of using a tuple.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
0

Leading zeros is just for data representation:

"{0:b}".format(4).zfill(8)

You can change XOR with other bit-wise operations:

def xor(x, y):
    return (~x & y) | (~y & x)

def bool_xor(x, y):
    return ((not x) and y) or ((not y) and x)

Actually, you can express all bitwise operations with just one logical operation: http://en.wikipedia.org/wiki/Functional_completeness

petraszd
  • 4,249
  • 1
  • 20
  • 12
-1

Since the INT type is a number it will be stored without leading zeros. Why would you want to store 675 as 00675? That's meaningless in the realm of integers. I would suggest storing the integers as integers and then only apply the padding when you access them and print them out (or whatever you are doing with them)

Josiah
  • 4,754
  • 1
  • 20
  • 19