2015-11-06 212 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"); 
    }