intptr\u t/uintptr\u t的atoi()等价物 C++中C++是否有一个函数(如果它有区别),它将一个字符串转换为 uTunptrt t 或我总是可以使用atol()并在以后强制转换它,但是如果能得到一个函数,它可以为32位机器执行32位操作,为64位机器执行64位操作,那就太好了 char* c = "1234567"; uintptr_t ptr = atoptr(c); // a function that does this; 这是一个IMO惊人的差距在C++中。虽然stringstream完成了这项工作,但对于如此简单的任务来说,它是一个相当繁重的工具。相反,您可以编写一个内联函数,根据类型大小调用strtoul的正确变体。因为编译器知道正确的大小,所以它将足够聪明,可以用对strtoul或strtoull的调用替换对函数的调用。例如,类似于以下内容: inline uintptr_t handleFromString(const char *c, int base = 16) { // See if this function catches all possibilities. // If it doesn't, the function would have to be amended // whenever you add a combination of architecture and // compiler that is not yet addressed. static_assert(sizeof(uintptr_t) == sizeof(unsigned long) || sizeof(uintptr_t) == sizeof(unsigned long long), "Please add string to handle conversion for this architecture."); // Now choose the correct function ... if (sizeof(uintptr_t) == sizeof(unsigned long)) { return strtoul(c, nullptr, base); } // All other options exhausted, sizeof(uintptr_t) == sizeof(unsigned long long)) return strtoull(c, nullptr, base); }

intptr\u t/uintptr\u t的atoi()等价物 C++中C++是否有一个函数(如果它有区别),它将一个字符串转换为 uTunptrt t 或我总是可以使用atol()并在以后强制转换它,但是如果能得到一个函数,它可以为32位机器执行32位操作,为64位机器执行64位操作,那就太好了 char* c = "1234567"; uintptr_t ptr = atoptr(c); // a function that does this; 这是一个IMO惊人的差距在C++中。虽然stringstream完成了这项工作,但对于如此简单的任务来说,它是一个相当繁重的工具。相反,您可以编写一个内联函数,根据类型大小调用strtoul的正确变体。因为编译器知道正确的大小,所以它将足够聪明,可以用对strtoul或strtoull的调用替换对函数的调用。例如,类似于以下内容: inline uintptr_t handleFromString(const char *c, int base = 16) { // See if this function catches all possibilities. // If it doesn't, the function would have to be amended // whenever you add a combination of architecture and // compiler that is not yet addressed. static_assert(sizeof(uintptr_t) == sizeof(unsigned long) || sizeof(uintptr_t) == sizeof(unsigned long long), "Please add string to handle conversion for this architecture."); // Now choose the correct function ... if (sizeof(uintptr_t) == sizeof(unsigned long)) { return strtoul(c, nullptr, base); } // All other options exhausted, sizeof(uintptr_t) == sizeof(unsigned long long)) return strtoull(c, nullptr, base); },c++,atoi,string-conversion,C++,Atoi,String Conversion,如果您决定更改句柄的类型,这将很容易更新。如果你喜欢尖括号,你也可以用模板做一些等价的事情,尽管我不知道这怎么会更清楚 最后,您还可以将sscanf与%tx格式一起使用,即 inline uintptr_t handleFromString(const char *c) { ptrdiff_t h; sscanf(c, "%tx", &h); // only hex supported, %td for decimal. return (uintpt

如果您决定更改句柄的类型,这将很容易更新。如果你喜欢尖括号,你也可以用模板做一些等价的事情,尽管我不知道这怎么会更清楚

最后,您还可以将
sscanf
%tx
格式一起使用,即

inline uintptr_t handleFromString(const char *c)
{
   ptrdiff_t h;
   sscanf(c, "%tx", &h); // only hex supported, %td for decimal.
   return (uintptr_t)h;
}

不幸的是,我在编译器资源管理器上尝试过的编译器都没有以消除对sscanf调用的方式优化代码。

您尝试过std::istringstream吗?我不确定将字符串读取为范围取决于平台的“int”是一个好主意。由于atoi()肯定不是一个在现实生活中使用的函数,所以您回到了strtol,它总是返回一个长的函数。顺便问一下,你真的在读字符串中的指针吗???@开玩笑的是,这尤其适用于windows编程;我正在使用
bInheritHandles
创建一个进程,显然是通过命令行传递句柄的值来告诉子进程句柄是什么。现在我试图将它解析回句柄(声明为
void*
)。@TonyD我将查看
istringstream
tbh我以前没有使用过它,但它看起来可能是我想要的。@atanamir只是“std::istringstream iss(“1234567”);如果(iss>>std::hex>>ptr)…转换成功-如果是十进制,则删除十六进制。标准的哪一部分证明了
static\u assert()
?我看不到任何要求
std::uintpttr\u t
与任何其他类型的大小相同的内容。静态断言的存在正是因为函数可能不会用尽所有可能。这意味着该计划必须改变。对我来说,这就是
static\u assert
的目的,因此我没有明确说明这一点,但我会将注释状态设置为这样。谢谢这个函数的用例是什么?我试图使函数中的注释更加清晰。这样做的目的是能够对原始post中描述的字符串到
uintpttr\t
的转换使用相同的函数,同时仍然按照原始post中的要求始终为目标平台上的实际数据类型调用适当的函数。我认为这是一个学术性的问题,但我可以提出一些用例,比如嵌入式目标,您可能不需要像stringstream这样沉重的东西。即使是“call strtoull后跟typecast”而不是“strtoul”也可能太重了。