Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.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/8/python-3.x/16.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
如何转换';0x61';在python中设置为0x61_Python_Python 3.x - Fatal编程技术网

如何转换';0x61';在python中设置为0x61

如何转换';0x61';在python中设置为0x61,python,python-3.x,Python,Python 3.x,我使用hex(ord('a'))并获取“0x61”值作为字符串。但我需要得到一个不带引号的整数0x61。我如何在我的代码中做到这一点 编辑1.0: import ctypes from ctypes import wintypes user32 = ctypes.WinDLL('user32', use_last_error=True) INPUT_MOUSE = 0 INPUT_KEYBOARD = 1 INPUT_HARDWARE = 2 KEYEVENTF_EXTENDEDKEY =

我使用
hex(ord('a'))
并获取“0x61”值作为字符串。但我需要得到一个不带引号的整数0x61。我如何在我的代码中做到这一点

编辑1.0:

import ctypes
from ctypes import wintypes

user32 = ctypes.WinDLL('user32', use_last_error=True)

INPUT_MOUSE = 0
INPUT_KEYBOARD = 1
INPUT_HARDWARE = 2

KEYEVENTF_EXTENDEDKEY = 0x0001
KEYEVENTF_KEYUP = 0x0002
KEYEVENTF_UNICODE = 0x0004
KEYEVENTF_SCANCODE = 0x0008

MAPVK_VK_TO_VSC = 0

# msdn.microsoft.com/en-us/library/dd375731
VK_TAB = 0x09
VK_MENU = 0x12

# C struct definitions
wintypes.ULONG_PTR = wintypes.WPARAM

class MOUSEINPUT(ctypes.Structure):
_fields_ = (("dx", wintypes.LONG),
            ("dy", wintypes.LONG),
            ("mouseData", wintypes.DWORD),
            ("dwFlags", wintypes.DWORD),
            ("time", wintypes.DWORD),
            ("dwExtraInfo", wintypes.ULONG_PTR))

class KEYBDINPUT(ctypes.Structure):
_fields_ = (("wVk", wintypes.WORD),
            ("wScan", wintypes.WORD),
            ("dwFlags", wintypes.DWORD),
            ("time", wintypes.DWORD),
            ("dwExtraInfo", wintypes.ULONG_PTR))

def __init__(self, *args, **kwds):
    super(KEYBDINPUT, self).__init__(*args, **kwds)
    # some programs use the scan code even if KEYEVENTF_SCANCODE
    # isn't set in dwFflags, so attempt to map the correct code.
    if not self.dwFlags & KEYEVENTF_UNICODE:
        self.wScan = user32.MapVirtualKeyExW(self.wVk,
                                             MAPVK_VK_TO_VSC, 0)

class HARDWAREINPUT(ctypes.Structure):
_fields_ = (("uMsg", wintypes.DWORD),
            ("wParamL", wintypes.WORD),
            ("wParamH", wintypes.WORD))

class INPUT(ctypes.Structure):
class _INPUT(ctypes.Union):
    _fields_ = (("ki", KEYBDINPUT),
                ("mi", MOUSEINPUT),
                ("hi", HARDWAREINPUT))

_anonymous_ = ("_input",)
_fields_ = (("type", wintypes.DWORD),
            ("_input", _INPUT))

LPINPUT = ctypes.POINTER(INPUT)

def _check_count(result, func, args):
if result == 0:
    raise ctypes.WinError(ctypes.get_last_error())
return args

user32.SendInput.errcheck = _check_count
user32.SendInput.argtypes = (wintypes.UINT,  # nInputs
                         LPINPUT,  # pInputs
                         ctypes.c_int)  # cbSize

# Functions
def PressKey(hexKeyCode):
x = INPUT(type=INPUT_KEYBOARD,
          ki=KEYBDINPUT(wVk=hexKeyCode))
user32.SendInput(1, ctypes.byref(x), ctypes.sizeof(x))

def ReleaseKey(hexKeyCode):
x = INPUT(type=INPUT_KEYBOARD,
          ki=KEYBDINPUT(wVk=hexKeyCode,
                        dwFlags=KEYEVENTF_KEYUP))
user32.SendInput(1, ctypes.byref(x), ctypes.sizeof(x))

change_symbols_dictionary = {
'ф': 0x441,
'и': 0x442,
'с': 0x443,
'в': 0x444,
'у': 0x445,
'а': 0x446,
'п': 0x447,
'р': 0x448,
'ш': 0x449,
'о': 0x43e,
'л': 0x43b,
'д': 0x434,
'ь': 0x44c,
'т': 0x442,
'щ': 0x449,
'з': 0x450,
'й': 0x451,
'к': 0x452,
'ы': 0x453,
'е': 0x454,
'г': 0x455,
'м': 0x456,
'ц': 0x457,
'ч': 0x458,
'н': 0x459,
'я': 0x44f,
'ё': 0x451,
'х': 0x445,
'ъ': 0x44a,
'ж': 0x436,
'э': 0x44d,
'б': 0x431,
'ю': 0x44e,
' ': 0x20,
'-': 0x2d,
}

如果我想对俄语符号使用虚拟键,我需要传递一个int表示,而不是字符串。我从这里得到了它

试着运行下面的行,将字符串“0x61”转换为十六进制等效值,其十进制值为97:

int('0x61', 16)
这表示您希望将字符串“0x61”解释为基数为16的整数。您可能会注意到,这与简单地执行ord('a')完全相同


在这两种情况下,如果打印输出,输出将以10为基数进行视觉格式化,但其值相同。

ord('a')
返回一个整数:
ord('a')
已经是所需的int。ints
97
0x61
之间没有区别。如果要将其打印为
0x61
,则在打印时应将其转换为十六进制字符串。@user2357112我使用
ctypes
,其中声明的def
按key(hexKeyCode)
。我需要这样称呼它:
按key(0x61)
,而不是
按key('0x61')
。我现在需要做什么?首先生成十六进制字符串,然后从中解析出一个int是毫无意义的。@user2357112据我们所知,OP可能从web服务响应接收到一段字符串编码的数据,他们需要将其解码为十六进制数才能“理解”并正确处理这些信息。我认为OP只是对打印整数的默认格式感到困惑。它是相同的值,但显示为97,而不是0x61。伙计们,我知道,它是相同的,我只是不知道如何传递0x61,而不是97。@OlegSkidan如果它需要一个整数,就传递97。它与0x61完全相同,类型也相同。如果需要字符串,请传递“0x61”。