1

I am using sage maths (which uses Python(more or less)) to do some work involving cryptography. This involves using binary keys which you xor to generate a cipher or message. Often, the input data is nn hex and so I have to convert to binary to see what is going on. Here is an example of the kind of thing I am doing once I have my binary.

my_cip=0b01101.....
my_mes=0b0110....
ot_mes=0b011...
my_key=0b0000....
key_fn=0b00....
altcip=0b011011..
print my_key.__xor__(ot_mes).binary()

The problem is that when I use utility functions to convert ascii to binary and hex to binary, the result has any leading zeroes thrown away.

I do not know how many have been thrown away so I have been predicting how many bits should be in the result and sticking the appropriate number of zeroes on the front of the result.

Here is my question! Is there a way of forcing the leading zeroes to be included in the result of calling (for example) ?

`print Integer(0x6c73.....).binary() ?`
nerak99
  • 640
  • 8
  • 26

2 Answers2

2

Use format()

See documentation Python format documentation

crow16384
  • 587
  • 3
  • 15
  • Thank you for your contribution. zmbq drilled down to the specific answer that I was after which is a subset of format. – nerak99 Nov 25 '12 at 18:39
1

You can find a similar issue here. It shows you how to pad zeroes very nicely.

Community
  • 1
  • 1
zmbq
  • 38,013
  • 14
  • 101
  • 171