Home > Back-end >  converting hex string to an integer while preserving the format
converting hex string to an integer while preserving the format

Time:12-28

Title might be aids I don't really know what this process is called. Anyway basically I am using JSON to config my tool. (which is wrote in C ). but I want to configure key binds for GetAsyncKeyState but JSON doesn't support hex which I need for the virtual key codes. A solution for this is to use a string then convert it back to an int. However no method i have found does this properly.

here's some pseudocode for the expected output

string str = "0x01";

int i = 0;

// here i should be converted

std::cout << std::hex << i << std::endl; // this should output 0x01

CodePudding user response:

unsigned int i = std::stoul(str, nullptr, 16);

CodePudding user response:

Virtual key codes are not "hex numbers". There are no such things as hex numbers. There are just numbers, integers, and you can represent them in various ways in source code and in text files.

There is no reason to represent a virtual key code as hex in JSON just because Microsoft represents them as hex in their header files. For example the virtual key code for the escape key, VK_ESCAPE, is 0x1B. Hex 1B is 27 in decimal (1 * 16 11) so just put 27 in your JSON for escape.

  • Related