-1

I use hex(ord('a')) and get '0x61' value as string. But I need to get it like an integer 0x61 without quotes. How can I do this inside my code?

Edit 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,
}

If I want use virtual key for Russian Symbols, I need pass an int representation, not a string. I got it from here How to generate keyboard events in Python?

Oleg Skidan
  • 617
  • 6
  • 24
  • 3
    `ord('a')` returns an integer: https://docs.python.org/2/library/functions.html#ord – Hunter McMillen Aug 15 '17 at 18:42
  • 3
    `ord('a')` is already the int you need. There is *no difference* between the ints `97` and `0x61`. If you want to print it as `0x61`, you should convert it to a hexadecimal string when printing. – user2357112 Aug 15 '17 at 18:44
  • @user2357112 I use `ctypes`, where declared def `PressKey(hexKeyCode)`. I need to call it like this: `PressKey(0x61)`, not `PressKey('0x61')`. What I need to do now? – Oleg Skidan Aug 15 '17 at 18:57

1 Answers1

-1

Try running the following line to convert the string '0x61' into the hexidecimal equivalent, which has a decimal value of 97:

int('0x61', 16)

This says that you want to interpret the string '0x61' as a base-16 integer. You may notice that this is exactly the same as simply doing ord('a').

In both cases, the output will be visually formatted in base 10 if you print it out, but it is the same value.

Brian Schmitz
  • 1,023
  • 1
  • 10
  • 19
  • Kind of pointless to produce the hex string in the first place only to parse an int back out of it. – user2357112 Aug 15 '17 at 18:49
  • 1
    @user2357112 for all we know OP might be receiving a string-encoded piece of data from a web service response and they need it decoded as a hex number to "understand" and properly process this information. – jathanasiou Aug 15 '17 at 18:52
  • 1
    I think the OP is simply confused about the default formatting of printing out the integer. It's the same value, but appears as 97 and not 0x61. – Brian Schmitz Aug 15 '17 at 18:52
  • Guys, I know, that it is the same, I just don't know how pass 0x61, not 97. – Oleg Skidan Aug 15 '17 at 19:00
  • 1
    @OlegSkidan If it is expecting an integer, pass 97. It is literally the same bit-for-bit as 0x61 and the same type. If it is expecting a string, pass '0x61'. – Brian Schmitz Aug 15 '17 at 19:14