Note that there are some explanatory texts on larger screens.

plurals
  1. POPython Force python to keep leading zeros of int variables
    text
    copied!<p>Below is a section of code which is part of a functional decryption and encryption program.</p> <pre><code>while checkvar &lt; maxvar: # is set to &lt; as maxvar is 1 to high for the index of var #output.append("%02d" % number) i =ord(var[checkvar]) - 64 # Gets postional value of i i = ("%02d" % i) if (checkvar + 1) &lt; maxvar: j =ord(var[(checkvar + 1)]) - 64 # Gets postional value of i j = ("%02d" % j) i = str(i) + str(j) #'Adds' the string i and j to create a new i li.append(int(i)) checkvar = checkvar + 2 print li </code></pre> <p>As you can see the two variables i and j are first treated as string to add a 0 in front of any single digit numbers (as string). These variables then are combined to make a four digit number (still as a string). Later in the program the number created are used in a pow function. AS ints remove any leading zeros.</p> <p>My question: Is it possible to force python to keep the leading zero for ints. I have and continuing to search online.</p> <p>EDIT</p> <p>TO help people I have included the encrpytion part of the program. This is where the problem lies. The variables created in the above code are passed through a pow() function. As this can't handle strings i have to convert the variables to ints where the leading zero is lost.</p> <pre><code>#a = li[] b=int(17)#pulic = e c=int(2773)#=n lenli=int(len(li)) enchecker = int(0) #encrpted list enlist = [] while enchecker &lt; lenli: en = pow(li[enchecker],b,c)#encrpyt the message can only handle int enlist.append(int(en)) enchecker = enchecker + 1 print enlist </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload