2015-11-06 214 views
1

對於我正在嘗試編寫的應用程序,我需要能夠在接口中編寫GLEnable(GL_REPEAT)(獲得此工作)。 一旦用戶這樣做,系統應該使用正確的參數調用該函數。將一個字節的字符串轉換爲一個unsigned int

到目前爲止,我已經得到了正確的函數被調用

((void (*)(unsigned int)) FunctionName)(Parameter);但隨着0

爲了得到正確的參數的參數,我讀了glew.h文件作爲文本文件,並將其解析爲std :: map。但是,我堅持如何將0x2901(和其他)從字符串轉換爲無符號整型。如果有人碰巧知道該怎麼做,幫助將不勝感激:)提前

感謝,

喬伊

+0

[標準:: stoul](http://www.cplusplus.com/reference/string/stoul/)? –

回答

0

也許你可以使用一個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

Live Demo

+0

謝謝,這個作品很棒:) 儘管如此,它仍然沒有工作,但這是由於帶參數的void *函數。這部分工作:) –

+0

@JoeyvanGangelen:很高興它的作品。如果它解決了你的問題,請接受這個答案。 如果你有關於你的'void *'函數的另一個問題,你可以考慮詢問一個單獨的問題。 – AndyG

+0

我是新來的stackoverflow ..我如何接受答案?我在左邊勾選了它,但找不到「已解決的標記」或類似的東西。 –

0

你也可以試試空調風格(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"); 
    }