Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python十六进制()的问题_Python_Python 2.7_Pyusb - Fatal编程技术网

python十六进制()的问题

python十六进制()的问题,python,python-2.7,pyusb,Python,Python 2.7,Pyusb,我有如下json文件 { "devices" :[ { "Manufacturer name": "Sony", "vendor_id":"8087" , "product_id" : "07da" }, { "Manufacturer name": "

我有如下json文件

{
   "devices"  :[
                 {
                 "Manufacturer name": "Sony",
                 "vendor_id":"8087" ,
                 "product_id" : "07da"
                 },
                 {
                 "Manufacturer name": "Sandisk",
                 "vendor_id": "1d6b",
                 "product_id" : "0002"
                 },
                 {
                 "Manufacturer name": "Chicony Electronics",
                 "vendor_id": "04f2",
                 "product_id" : "b381"
                 }
               ]

}
此json文件包含连接到我的笔记本电脑的usb设备的供应商和产品id。我正在检查usb设备是否使用此json文件连接到笔记本电脑。供应商和产品id位于
hex
中。由于
json
不能使用
hex
,因此我已经用字符串格式编写了这些值。实际上,我正在使用python的
pyusb
模块检查设备的连接,如下所示

import usb.core
def get_hex(hex_str):
    hex_int = int(hex_str, 16)
    return hex(hex_int)

vendor_id = get_hex("8087")
product_id = get_hex("07da")
dev = usb.core.find(idVendor=vendor_id, idProduct=product_id)

if dev is None:
    print "Disconnected"
else:
    print "Connected"
但当我运行此代码时,会收到打印消息,显示为“断开连接”
实际上,这里的问题是
usb.core.find()
函数需要
int
中的值,但是
get\u hex()
函数返回的值是
string
。一旦将上述代码中的行
dev=usb.core.find(idVendor=vendor\u id,idProduct=product\u id)
更改为
dev=usb.core.find(idVendor=0x8087,idProduct=0x07da)
。上面的代码工作正常。请告诉我如何从
int
中的
get_hex()
返回值。问题是您正在将十六进制字符串转换为int,然后再转换回十六进制字符串。在将其转换为int后返回。最好将函数重命名为
get_int
,因为您正在将其转换为int

def get_int(hex_str):
    return int(hex_str, 16)

vendor_id = get_int("8087")
product_id = get_int("07da")
dev = usb.core.find(idVendor=vendor_id, idProduct=product_id)

问题是您正在将十六进制字符串转换为int,然后再转换回十六进制字符串。在将其转换为int后返回。最好将函数重命名为
get_int
,因为您正在将其转换为int

def get_int(hex_str):
    return int(hex_str, 16)

vendor_id = get_int("8087")
product_id = get_int("07da")
dev = usb.core.find(idVendor=vendor_id, idProduct=product_id)
可能的重复可能的重复