[英]Converting a string of a byte to an unsigned int
對於我要編寫的應用程序,我需要能夠在接口中編寫GLEnable(GL_REPEAT)(完成此工作)。 用戶這樣做后,系統應使用正確的參數調用該函數。
到目前為止,我已經調用了正確的函數
((void (*)(unsigned int)) FunctionName)(Parameter);
但參數為0。
為了獲得正確的參數,我將glew.h文件讀取為文本文件,並將其解析為std :: map。 但是,我對如何將0x2901(及其余)從字符串轉換為無符號int感到困惑。 如果有人碰巧知道如何做到這一點,將不勝感激幫助:)
提前致謝,
喬伊
也許您可以使用std::stringstream
:
std::string hexString = "0x2901";
std::istringstream instream(hexString);
unsigned int receiver = 0;
instream >> std::hex >> receiver;
std::cout << "Value parsed: " << receiver << std::endl;
std::cout << "Should be 10497" << std::endl;
輸出:
解析值:10497
應該是10497
您也可以嘗試使用C樣式( sscanf
函數),如下所示:
std::string hex = "0x2901";
unsigned int x;
sscanf(hex.c_str(), "%x", &x);
printf("%#X = %u\n", x, x);
sscanf
允許檢查以下樣式:
std::string hex = "0x2901";
unsigned int x = 0;
if ( sscanf(hex.c_str(), "%x", &x) == 1)
{
printf("%#X = %u\n", x, x);
}
else
{
printf("Incorrect string value\n");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.