我正在使用回调(使用glfwSetCharCallback设置)从GLFW读取用户输入,根据这个页面:char
回调函数将按下的键接收为32位无符号int。我如何将它转换成可以打印在屏幕上的东西?我尝试过从C++11和ICU库中提取代码,但无法将可读的字符打印到我的终端。
这是我的回调函数的代码:
void InputManager::charInputCallback(GLFWwindow* window, unsigned int key)
{
UErrorCode errorStatus = U_ZERO_ERROR; //initialize to no error
UConverter *utf32toUnicode = ucnv_open("UTF-32", &errorStatus); //create converter utf-32 <=> Unicode
if(U_FAILURE(errorStatus))
std::cout << u_errorName(errorStatus) << std::endl;
UChar unicode[10] = {0};
ucnv_toUChars(utf32toUnicode, unicode, 10, (char*)key, 4, &errorStatus); // this fails with an U_ILLEGAL_ARGUMENT_ERROR
if(U_FAILURE(errorStatus))
std::cout << u_errorName(errorStatus) << std::endl;
std::cout << unicode << std::endl;
}如果我对输入(键)不做任何操作,则不会显示任何内容。只是一个空白行。
发布于 2016-09-07 21:38:24
您正在调用ucnv_toUChars(),它将8位字符串转换为Unicode。但是您需要另一种方式,将Unicode字符串转换为8位,例如UTF-8。我将使用UnicodeString类,它有一个以UTF-32为构造函数的构造函数和一个方法toUTF8String()。
https://stackoverflow.com/questions/39376089
复制相似问题