繁体   English   中英

如何修复int在python中不可迭代?

[英]How to fix int is not iterable in python?

我以十六进制输入pesan。 例如 e = 5、n = 221 和 pesan = 1e。 然后 pesan 将转换为十进制并解密。 结果(十进制)将再次转换为十六进制。 但我有错误

TypeError: 'int' object is not iterable.

如何解决? 这是代码

def mod(x,y):
    if (x < y):
        return x
    else:
        c = x % y
        return c

def hex_to_decimal(hex_str):
    decimal_number = int(hex_str, 16)
    return decimal_number
                 
def decimal_to_hex(decimal_str):
    decimal_number = int(decimal_str, 10)
    hex_number = hex(decimal_number)[2:]
    return hex_number

def decrypt(m):
    plainDecimal=[]
    for i in m:
        cipherElement=mod(int(i)**e,n)
        plainDecimal.append(cipherElement)
    return plainDecimal

e = int(input('Input e: '))
n = int(input('Input n: '))
pesan = input('Input message: ' )
decimalp = hex_to_decimal(pesan) #Convert hex to decimal
plain = decrypt(decimalp)
ListToStr2 = ''.join([str(x) for x in plain])
decimal1 = decimal_to_hex(ListToStr2) #Convert decimal to hex
print("Dekripsi Tanda Tangan Digital (Heksadesimal): ", decimal1)

在您给出的小数点值是一个整数的代码中,我现在使用 str() 函数将小数点值更改为字符串。 该代码现在应该可以完美运行。 更正后的代码如下;

def mod(x,y):
    if (x < y):
        return x
    else:
        c = x % y
        return c

def hex_to_decimal(hex_str):
    decimal_number = int(hex_str, 16)
    return decimal_number
                 
def decimal_to_hex(decimal_str):
    decimal_number = int(decimal_str, 10)
    hex_number = hex(decimal_number)[2:]
    return hex_number

def decrypt(m):
    plainDecimal=[]
    for i in m:
        cipherElement=mod(int(i)**e,n)
        plainDecimal.append(cipherElement)
    return plainDecimal

e = int(input('Input e: '))
n = int(input('Input n: '))
pesan = input('Input message: ' )
decimalp = str(hex_to_decimal(pesan)) #Convert hex to decimal
plain = decrypt(decimalp)
ListToStr2 = ''.join([str(x) for x in plain])
decimal1 = decimal_to_hex(ListToStr2) #Convert decimal to hex
print("Dekripsi Tanda Tangan Digital (Heksadesimal): ", decimal1)

我已经解决了这个问题。 这是代码

def mod(x,y):
    if (x < y):
        return x
    else:
        c = x % y
        return c

def hex_to_decimal(hex_str):
    decimal_number = int(hex_str, 16)
    return decimal_number
                 
def decimal_to_hex(decimal_str):
    decimal_number = int(decimal_str, 10)
    hex_number = hex(decimal_number)[2:]
    return hex_number

def decrypt(m):
    m_str = str(m)
    decimalList =[int(m_str)]
    plainDecimal=[]
    for i in decimalList:
        cipherElement = mod(int(i)**e,n)
        plainDecimal.append(cipherElement)
    return plainDecimal

e = int(input('Input e: '))
n = int(input('Input n: '))
pesan = input('Input message: ' )
decimalp = str(hex_to_decimal(pesan)) #Convert hex to decimal
plain = decrypt(decimalp)
ListToStr2 = ''.join([str(x) for x in plain])
decimal1 = decimal_to_hex(ListToStr2) #Convert decimal to hex
print("Dekripsi Tanda Tangan Digital (Heksadesimal): ", decimal1)

暂无
暂无

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

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