【问题标题】:Is there a way to convert a number to a series of virtual key-codes?有没有办法将数字转换为一系列虚拟键码?
【发布时间】:2020-04-19 02:33:52
【问题描述】:

例如,有没有一种方法可以让我执行以下操作?

//string converted = ConvertToVirtual(int numberToConvert);

string converted = ConvertToVirtual(12);

converted 然后将保持“0x31 0x32”。

理想情况下,传递给函数的数字可以是任意位数,但现在,我只是想知道这是否可能,我该如何去做?

非常感谢您提前(:

【问题讨论】:

  • 你指的这个“虚拟键码”是什么?在构成当前 C++ 标准的技术规范的两千页中,没有提到任何称为“虚拟键码”的东西。它是什么?如果它只是某种由单个数值引用的值列表,只需创建一个数组或它们的映射,然后让此函数查找并返回它。任务完成。
  • 将您的 int 转换为字符串,然后将字符串的每个字符转换为 int,然后将每个 int 转换为前面带有 0x 的字符串。
  • @JohnFilleau 转换为 int 后是否需要额外的步骤才能从十进制转换为十六进制
  • @BigRed118 当它是 int 时,它是二进制的。没有从十进制到十六进制再到八进制或任何东西。将十进制整数转换为十六进制整数会是什么样子?
  • @JohnFilleau 我只是说他希望为 1 显示 0x31,而不是 0x49。如果你只是拍打转换后的整数,它将是基数 10 而不是 16。我所说的只是确保它在基数 16 中。

标签: c++ keycode


【解决方案1】:

这样的事情应该可以工作:

#include <ios>

std::string ConvertToVirtual(int num)
{
    std::string numStr = std::to_string(num);
    std::stringstream output;
    output << std::hex; // this will convert all following data to hex
    for (const char& c : str)
    {
        output << "0x" << (int)c << " "; 
    }
    return output.str(); // extract string from stringstream
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-08
    • 1970-01-01
    • 2012-01-06
    • 1970-01-01
    • 2020-05-20
    相关资源
    最近更新 更多