atoi() equivalent for intptr_t/uintptr_t
atoi() equivalent for intptr_t/uintptr_t
C++(C++11,如果有区别的话)中是否有一个函数可以将字符串转换为uintptr_t
或intptr_t
?我总是可以使用atoll()
并在之后进行强制转换,但如果能得到一个对32位机器执行32位操作、对64位机器执行64位操作的函数,那就太好了。
char* c = "1234567";
uintptr_t ptr = atoptr(c); // a function that does this;
这是国际海事组织在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);
}
如果您决定更改句柄的类型,这将很容易更新。如果你喜欢尖括号,你也可以用模板做一些等效的事情,尽管我不知道怎么会更清楚。
最后,您还可以使用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调用的方式优化代码。
相关文章:
- 我无法从 C# 获取 PInvoke 的输入参数以C++ DLL 以用作 IntPtr 的输出
- C++到 C# 转换与 IntPtr
- CreateCompatibleDC(IntPtr.Zero) returns IntPtr.Zero
- 如何让 IntPtr 成为本机 directx11 结构?
- 使用安全模式从DLL(通过(INTPTR)参数传递到该函数)获取char **
- 初始化 C# IntPtr 以接受来自非托管 C++ DLL 的数据?
- C#P/Invoke:将INTPTR的枚举值传递到功能;AccessViolationException
- 如何处理从C#中的C DLL返回的INTPTR
- typedef int* intptr OR typedef int *intptr;
- 我们可以将所有"int"参数和返回类型替换为 DllImport 方法签名中的"IntPtr"吗?
- const int*& vs typedef int* IntPtr
- HANDLE(IntPtr)已过时,kernel32.dll,安全文件句柄到IntPtr
- IntPtr大小问题
- c++ to VB.Net IntPtr Strings
- ComPtr<ID3D11Device> to IntPtr
- 如何在 C# 中获取 IntPtr 指针的属性
- 如何将 void* 放入 COM 变体中,以便它被 COM-Interop 编组为 IntPtr,到 .NET
- WP8:如何在 c++ 中找回 IntPtr
- 将 IntPtr 转换为对象 C#
- 无法使用 Marshal.PtrToStringAuto(IntPtr) 将 C++ char[] 封送到 C# 字符串