如何转换' 0x61'在python中为0x61

时间:2017-08-15 18:40:18

标签: 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 = 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表示,而不是字符串。我从这里得到了How to generate keyboard events in Python?

1 个答案:

答案 0 :(得分:-1)

尝试运行以下行将字符串'0x61'转换为十六进制当量,其十进制值为97:

Option Explicit

Sub Counter()

Dim NoF As Long
Dim j As Long
Dim i As Long

NoF = Range("B1", Range("B1").End(xlDown)).Count
j = 0

For i = 1 To NoF

    If Cells(i, 1) = "" Then
        Cells(i, 1).Value = i - j
    Else
        j = j + 1
    End If

Next i

End Sub

这表示你想将字符串'0x61'解释为base-16整数。您可能会注意到这与简单地执行ord('a')完全相同。

在这两种情况下,如果您将输出打印出来,输出将在底部10中以可视方式格式化,但它的值相同。