简体   繁体   English

如何保留 int 变量的前导零?

[英]How to retain leading zeros of int variables?

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

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).如您所见,首先将 i 和 j 这两个变量视为字符串,以便在任何单个数字(作为字符串)前添加 0。 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.在程序的后面,创建的数字用于pow() function,因为int s 删除了所有前导零。

My question: Is it possible to force python to keep the leading zero for int s?我的问题:是否可以强制 python 保留int的前导零? I have and continued to search online.我已经并将继续在线搜索。

Edit编辑

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.在上面的代码中创建的变量通过pow() function 传递。由于这无法处理字符串,我必须将变量转换为int s,其中前导零丢失。

#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

Though the comments above are true regarding 1, 01, and 001, are all the same as an int , it can be very helpful in temporal modeling, or sequential movie making to maintain the leading zeros.尽管上面关于 1、01 和 001 的评论是正确的,都与int相同,但它在时间建模或序列电影制作中非常有帮助,以保持前导零。 I do it often to ensure movie clips are in proper order.我经常这样做以确保影片剪辑的顺序正确。 The easy way to do that is using zfill() to ensure the str version of the number has at least the number of characters you tell it, and does so by filling in the left-side of the string "number" with zeros.做到这一点的简单方法是使用zfill()来确保数字的str版本至少具有您告诉它的字符数,并通过用零填充字符串“数字”的左侧来实现。

>>> x = int(1)    
>>> NewStringVariable = str(x).zfill(3)    
>>> print NewStringVariable    
001    
>>> NewStringVariable = str(x).zfill(5)    
>>> print NewStringVariable    
00001

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.如果需要,您可以单独保留该号码。

I was getting date strings like hhmmss coming across the serial line from my Arduino.我从我的 Arduino 的串行线上收到像hhmmss这样的日期字符串。

so suppose I got s = "122041" ;所以假设我得到s = "122041" ; this would be 12:20:41 , however 9am would be 090000 .这将是12:20:41 ,但是9am将是090000

The statement print "%d" % (s) provokes a run time error because the 9 is not an octal number and is hence an illegal character.语句print "%d" % (s)会引发运行时错误,因为 9 不是八进制数,因此是非法字符。

To fix this problem:要解决此问题:

print "%06d" % (int(s))

Hope that helps希望有帮助

Try this:尝试这个:

number = 1
print("%02d" % (number,))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM