Python Force python保持int变量的前导零 [英] Python Force python to keep leading zeros of int variables

查看:184
本文介绍了Python Force python保持int变量的前导零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一段代码,它是功能解密和加密程序的一部分。

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功能。 AS ints删除任何前导零。

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.

我的问题:是否可以强制python保持int的前导零。我已经并继续在线搜索。

My question: Is it possible to force python to keep the leading zero for ints. I have and continuing to search online.

编辑

为了帮助我已经包含加密部分的人程序。这就是问题所在。在上面的代码中创建的变量通过pow()函数传递。由于这不能处理字符串,我必须将变量转换为丢失前导​​零的整数。

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.

#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.

这篇关于Python Force python保持int变量的前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆