在python中将uint8转换为int64

在python中将uint8转换为int64,python,numpy,type-conversion,Python,Numpy,Type Conversion,我有一个uint8类型的变量(它只有两个值,0和1),我想用-1替换零。我是否必须首先将此变量转换为int64(或任何允许负数的整数数据类型)?如果是这样,我如何实现它呢?您不需要int64。int8应该可以。要将uint8转换为int8,请执行以下操作: import numpy as np x = np.uint8(-1) # -1 does not exist in this coding, this is just a test x > 255 np.int8(x) > -1

我有一个uint8类型的变量(它只有两个值,0和1),我想用-1替换零。我是否必须首先将此变量转换为int64(或任何允许负数的整数数据类型)?如果是这样,我如何实现它呢?

您不需要int64。int8应该可以。要将uint8转换为int8,请执行以下操作:

import numpy as np
x = np.uint8(-1) # -1 does not exist in this coding, this is just a test
x
> 255
np.int8(x)
> -1 # eureka 

你能把它设置为255吗?你说你有一个
uint8
是什么意思,你是使用ctypes还是numpy?Python没有不同的整数类型,所以我不确定你指的是什么类型的东西。您应该显示一些您正在执行的操作的代码。我刚刚尝试用-1替换0,但它用255I替换了它。我正在使用numpy