简体   繁体   English

类型转换错误:Python

[英]Error in Type casting: Python

I have just started to learn python and I am getting the following error for the function I have written. 我刚刚开始学习python,并且由于编写的函数出现以下错误。 I think it's due to typecast but I am not sure how to correct. 我认为这是由于类型转换造成的,但我不确定如何更正。

Error I get: 错误我得到:

File "validate.py", line 23, in keygen
(array3[0]) = int(array2[0]) ^ int(array2[n-1],8)
ValueError: invalid literal for int() with base 10: '0xd4d745957e685d08L'

Function: 功能:

def keygen(array2):
    n = len(array2)
    array3[0] = int(array2[0]) ^ int(array2[n-1],8)
    i = 1
    while(i != n):
        array3[i] = array2[i] ^ array3[i-1]
        i = i + 1
    printarray(array3)

Here array2 is an array of hexadecimal value as below: 这里的array2是一个十六进制值的数组,如下所示:

 array2: [0xd4d745957e685d08L,
          0x9a542ff214aa011cL,
          0x1289806cf20e724L,
          0x20dd453feea65bf4L,
          0x209ab70c8a12a914L,
          0xa16cde82faf9de54L,
          0x99c60b68361506d4L,
          0x5b98ffaaf450a3d4L,
          0xe5c75e967b0bbbe9L]

array3 is an empty array where new values are inserted. array3是一个空数组,其中插入了新值。

Since array2 contains hex values you have to use int(array2[0], 16) to convert it into integer value. 由于array2包含十六进制值,因此必须使用int(array2 [0],16)将其转换为整数值。 you will need to remove the trailing L from the array or use something like below. 您需要从数组中删除结尾的L或使用类似下面的内容。

array3[0] = int(str(array2[0])[:18], 16) ^ int(str(array2[n-1])[:18], 16)

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

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