views:

97

answers:

4

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.

Many thanks, Jack

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 pseduocode 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've love to use the XOR operation but I'm afraid doing so I'm not going to learn what I need to.

-J

+8  A: 

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
ohhhh that's where I'm wrong. Integers don't have padding. I must've missed that tidbit somewhere. Thank you!
Jack Tranner
A: 

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  A: 

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
A: 

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