0
A
回答
2
为了得到一个阵列(或载体,如果你更喜欢)的地址的实际字节,这应该是诀窍:
int foo = 10;
int* bar = &foo;
// Interpret pointer as array of bytes
unsigned char const* b = reinterpret_cast<unsigned char const*>(&bar);
// Copy that array into a std::array
std::array<unsigned char, sizeof(void*)> bytes;
std::copy(b, b + sizeof(void*), bytes.begin());
获取包含十六进制表示的阵列分成单个字符(这使得任何意义上的),我会使用一个字符串流 - 因为有些人已经建议。你也可以使用snprintf来获取地址的字符串表示,但这更多的是C风格的方式。
// Turn pointer into string
std::stringstream ss;
ss << bar;
std::string s = ss.str();
// Copy character-wise into a std::array (1 byte = 2 characters)
std::array<char, sizeof(void*) * 2> hex;
std::copy(s.begin(), s.end(), hex.begin());
2
的最简单的方法是做
char buf[sizeof(void*) * 2 + 3];
snprintf(buf, sizeof(buf), "%p", /* the address here */);
0
void storeAddr(vector<string>& v,void *ptr)
{
stringstream s;
s << (void*)ptr ;
v.push_back(s.str());
}
+0
这里的演员什么都不做;它从'void *'投射到'void *' – Dave
1
1
C++方式DOS这是使用字符串流
#include <string>
#include <sstream>
int main()
{
MyType object;
std::stringstream ss;
std::string result;
ss << &object; // puts the formatted address of object into the stream
result = ss.str(); // gets the stream as a std::string
return 0;
}
相关问题
- 1. 将字符串的地址存储在字符串数组中
- 2. C# - WinRT - 将存储为字节数组的IPv6地址转换为字符串
- 3. C++ - 将BLOB存储为字符串
- 4. 为什么我可以在字符的内存地址中存储字符串?
- 5. 将输入字符串存储到C
- 6. C++存储字符串输入为cstring
- 7. 将字符串地址转换为int地址
- 8. c:在编译时将ipv6地址转换为字符串
- 9. C# - WinRT - 将IPv4地址从uint转换为字符串?
- 10. 如何将JAXBelement存储为字符串?
- 11. MATLAB将字符串存储为数组
- 12. 将XML存储为字符串
- 13. 将字符串存储在本地存储中
- 14. Mortorola 68k:如何将字符串ASCII存储在地址寄存器中a0
- 15. 存储XML为字符串
- 16. LINQ - 存储为字符串
- 17. C++地址字符串 - >长
- 18. Objective C字符串操作IP地址
- 19. 将字节值存储在字符串中?将字节转换为字符串?
- 20. 将dword存储到地址
- 21. C#将字符串变量存储为文本文件.txt
- 22. C++将修改后的MySQL时间戳存储为字符串
- 23. 如何将if语句存储为字符串? C#
- 24. 如何将字符串文字存储在内存中的c + +?
- 25. 如何使用逗号将字符串拆分为两个字符串并存储字符串? (C++)
- 26. 如何将UTF-16字符作为字符串存储在c#中?
- 27. 将列表变量存储为字符串并将其存储为变量
- 28. 在C中寻址字符串文字的地址
- 29. 存储字符串
- 30. 存储字符串
将每个字符即地址添加到向量中? – Dineshkumar
请参阅http://stackoverflow.com/questions/7850125/convert-this-pointer-to-string。 – user1929959
@Dineshkumar,现在更清楚了吗? – Subway