问题描述
下面是一段代码,是功能解密和加密程序的一部分.
Below is a section of code which is part of a functional decryption and encryption program.
while checkvar < maxvar: # is set to < 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) < 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
如您所见,两个变量i和j首先被视为字符串,以便在任何一位数字前添加一个0(作为字符串).然后将这些变量组合成一个四位数的数字(仍为字符串).在程序的后面,创建的数字用在pow()
函数中,因为int
删除所有前导零.
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 int
s remove any leading zeros.
我的问题:是否可以强制python将int
的前导零保持不变?我已经并且继续在线搜索.
My question: Is it possible to force python to keep the leading zero for int
s? I have and continued to search online.
修改
为帮助人们,我将程序的加密部分包括在内.这就是问题所在.上面代码中创建的变量通过pow()
函数传递.由于这无法处理字符串,因此我必须将变量转换为丢失前导零的int
s.
To help people I have included the encryption 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 int
s where the leading zero is lost.
#a = li[]
b=int(17)#pulic = e
c=int(2773)#=n
lenli=int(len(li))
enchecker = int(0)
#encrpted list
enlist = []
while enchecker < lenli:
en = pow(li[enchecker],b,c)#encrpyt the message can only handle int
enlist.append(int(en))
enchecker = enchecker + 1
print enlist
推荐答案
前导零的概念是一种显示概念,而不是数字概念.您可以在数字上放置无数个前导零而不更改其值.由于它不是数字概念,因此不会与数字一起存储.
The concept of leading zeros is a display concept, not a numerical one. You can put an infinite number of leading zeros on a number without changing its value. Since it's not a numeric concept, it's not stored with the number.
将数字转换为字符串时,您必须确定想要多少个零.您可以根据需要单独保留该号码.
You have to decide how many zeros you want when you convert the number to a string. You could keep that number separately if you want.
这篇关于如何保留int变量的前导零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!