如何在Python中进行类似Java的类型转换(byte)(Integer.parseInt(bytearrastr[i],16])?

如何在Python中进行类似Java的类型转换(byte)(Integer.parseInt(bytearrastr[i],16])?,python,casting,python-3.x,Python,Casting,Python 3.x,在Java中,我是这样做的,但在Python中如何做到这一点呢?特别是bytesArray[i]=(byte)(Integer.parseInt(byteArrayStr[i],16)) 公共静态字节[]toBytes(字符串字节)引发IOException { String byteArrayStr[]=bytes.split(“\\/”); 字节字节数组[]=新字节[byteArrayStr.length]; 对于(int i=0;i>>int('hello')) 回溯(最近一次呼叫最后一次

在Java中,我是这样做的,但在Python中如何做到这一点呢?特别是
bytesArray[i]=(byte)(Integer.parseInt(byteArrayStr[i],16))

公共静态字节[]toBytes(字符串字节)引发IOException
{
String byteArrayStr[]=bytes.split(“\\/”);
字节字节数组[]=新字节[byteArrayStr.length];
对于(int i=0;i直接回答:
int(x,16)
。您在Python中所做的是对单个列表的理解(我假设字符串看起来像
af/ce/13/…


直接回答:
int(x,16)
。您在Python中所做的是对单个列表的理解(我假设字符串看起来像
af/ce/13/…


只需对要强制转换的值“调用”类型

>>> stringlist = ['123', '245', '456']
>>> intlist = [int(e) for e in stringlist]
>>> intlist
[123, 245, 456]
如果该值对于您试图强制转换到的类型无效,则可能会导致VauleError异常:

>>> int('hello')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'hello'
>>>int('hello'))
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
ValueError:基数为10的int()的文本无效:“hello”

只需“调用”要转换的值的类型即可

>>> stringlist = ['123', '245', '456']
>>> intlist = [int(e) for e in stringlist]
>>> intlist
[123, 245, 456]
如果该值对于您试图强制转换到的类型无效,则可能会导致VauleError异常:

>>> int('hello')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'hello'
>>>int('hello'))
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
ValueError:基数为10的int()的文本无效:“hello”