You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

DirectInput十六进制键码字符串转short类型方法咨询

Got it, let's work through this problem step by step. You've got those hex string keycodes (like 0x1E for 'a') from your lookup array, and need to turn them into a short to feed into that external key simulation function. Here's how to handle this in the most common programming scenarios you're likely using with DirectInput:

C++ Approach

Since DirectInput is often paired with C++, this is probably your primary use case. You have two reliable options, depending on whether you prefer modern C++ or older C-style syntax:

Option 1: Modern C++ (C++11+)

Use std::stoi to parse the hex string, then cast the result to short (since std::stoi returns an int—a wider type than short—and DirectInput keycodes fit perfectly within the short range):

#include <string>

// Example string pulled from your lookup function
std::string hex_keycode = "0x1E";

// Parse the hex string and convert to short
short keycode = static_cast<short>(std::stoi(hex_keycode, nullptr, 16));

The 16 here is critical—it tells the function to interpret the string as a hexadecimal value. Skip this, and it'll default to decimal parsing, which will give you completely wrong results.

Option 2: C-Style (Compatible with Older C++)

If you're working with an older codebase, use strtol instead, and add optional error checking to validate the input:

#include <string>
#include <cstdlib>

std::string hex_keycode = "0x1E";
char* end_ptr;

// Parse the hex string to a long, then cast to short
long temp_value = strtol(hex_keycode.c_str(), &end_ptr, 16);
short keycode = static_cast<short>(temp_value);

// Optional: Verify the entire string was parsed correctly
if (*end_ptr != '\0') {
    // Handle invalid input (e.g., non-hex characters in the string)
}

The end_ptr lets you confirm that no invalid characters were left unparsed, which is helpful if your lookup array might have bad entries.

C# Approach

If you're using C# with a DirectInput wrapper, the process is equally straightforward:

// Example string from your lookup logic
string hexKeycode = "0x1E";

// Option 1: Direct conversion (throws an exception on invalid input)
short keycode = Convert.ToInt16(hexKeycode, 16);

// Option 2: Safe parsing with error handling
short keycode;
if (short.TryParse(hexKeycode, System.Globalization.NumberStyles.HexNumber, null, out keycode)) {
    // Use the valid keycode here
} else {
    // Handle invalid input (e.g., log an error, default to a safe value)
}

NumberStyles.HexNumber tells TryParse to accept the 0x prefix, and this method won't crash if it encounters a malformed string—always a smart choice for robust code.

Quick Note

DirectInput keycodes are designed to fit within a 16-bit integer (short), so you don't have to worry about overflow issues here as long as your lookup array contains valid keycodes. Just double-check that you're always parsing the string as hexadecimal, not decimal, and you'll be set.

内容的提问来源于stack exchange,提问作者MadeByVince

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:34:34