简体   繁体   English

在保留格式的同时将十六进制字符串转换为 integer

[英]converting hex string to an integer while preserving the format

Title might be aids I don't really know what this process is called.标题可能是辅助工具,我真的不知道这个过程叫什么。 Anyway basically I am using JSON to config my tool.无论如何,基本上我正在使用 JSON 来配置我的工具。 (which is wrote in C++). (这是用 C++ 编写的)。 but I want to configure key binds for GetAsyncKeyState but JSON doesn't support hex which I need for the virtual key codes.但我想为 GetAsyncKeyState 配置键绑定,但 JSON 不支持虚拟键码所需的十六进制。 A solution for this is to use a string then convert it back to an int.解决方案是使用字符串,然后将其转换回 int。 However no method i have found does this properly.但是,我发现没有一种方法可以正确地做到这一点。

here's some pseudocode for the expected output这是预期 output 的一些伪代码

string str = "0x01";

int i = 0;

// here i should be converted

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

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.没有理由在 JSON 中将虚拟键代码表示为十六进制,因为 Microsoft 在其 header 文件中将它们表示为十六进制。 For example the virtual key code for the escape key, VK_ESCAPE, is 0x1B.例如,转义键的虚拟键代码 VK_ESCAPE 是 0x1B。 Hex 1B is 27 in decimal (1 * 16 + 11) so just put 27 in your JSON for escape.十六进制 1B 是十进制的 27 (1 * 16 + 11),所以只需将 27 放入 JSON 即可转义。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM