为什么我的 C++ 到十六进制转换会导致不同的格式?
Why does my C++ to hexadecimal conversion cause different formatting?
我有一个内存地址作为 const void *
变量。为了将该内存地址打印为十六进制,我使用了以下函数:
inline void replace_all(std::string& text, const std::string& replace, const std::string& replacement)
{
if (replace.empty())
{
return;
}
size_t start_position = 0;
while ((start_position = text.find(replace, start_position)) != std::string::npos)
{
text.replace(start_position, replace.length(), replacement);
start_position += replacement.length();
}
}
template <typename T>
std::string to_hexadecimal(T value)
{
std::stringstream stream;
stream << std::hex;
stream << value;
auto output_string = stream.str();
replace_all(output_string, ",", "");
replace_all(output_string, ".", "");
replace_all(output_string, "\xA0", "");
// Truncate the leading zeros
while (output_string.at(0) == '0')
{
output_string = output_string.substr(1, output_string.size() - 1);
}
return output_string;
}
我想知道为什么我的软件的不同用户要求我添加 replace_all()
调用以替换十六进制地址输出中的所有逗号、点和空格(0xA0
字节)以确保正确行为。
没有 replace_all()
调用,用户的输出如下:
000,01D,992,8B0,000
000.01D.992.8B0.000
000 01D 992 8B0 000
但我真正一直想得到的是这样的:
1D9928B0000
我想知道为什么会出现不同类型的格式以及如何防止进一步的格式意外以确保输出始终具有相同的格式。
我调用 to_hexadecimal()
函数的方式是这样的:
const auto memory_address = reinterpret_cast<PVOID>(0x1234567812345678);
const auto hexadecimal_memory_address = to_hexadecimal(memory_address);
std::cout << hexadecimal_memory_address << std::endl;
你的意图还不能完全理解。插入器操作符已经超载以打印出指针。如果你有一个指针,不管是哪个,你可以简单地将地址插入到流中:
#include <iostream>
int main() {
double d = 3.14;
// Print address in hex
std::cout << &d << '\n';
return 0;
}
您的函数不起作用,因为您还允许通过模板传递任何类型。
如果类型是普通双精度数,则 stream << value;
不会将其转换为十六进制。尤其不是地址。十六进制与否,没关系。根本没有转换。
不过如前所述。所有这些都不需要。请使用上面的方法。
我有一个内存地址作为 const void *
变量。为了将该内存地址打印为十六进制,我使用了以下函数:
inline void replace_all(std::string& text, const std::string& replace, const std::string& replacement)
{
if (replace.empty())
{
return;
}
size_t start_position = 0;
while ((start_position = text.find(replace, start_position)) != std::string::npos)
{
text.replace(start_position, replace.length(), replacement);
start_position += replacement.length();
}
}
template <typename T>
std::string to_hexadecimal(T value)
{
std::stringstream stream;
stream << std::hex;
stream << value;
auto output_string = stream.str();
replace_all(output_string, ",", "");
replace_all(output_string, ".", "");
replace_all(output_string, "\xA0", "");
// Truncate the leading zeros
while (output_string.at(0) == '0')
{
output_string = output_string.substr(1, output_string.size() - 1);
}
return output_string;
}
我想知道为什么我的软件的不同用户要求我添加 replace_all()
调用以替换十六进制地址输出中的所有逗号、点和空格(0xA0
字节)以确保正确行为。
没有 replace_all()
调用,用户的输出如下:
000,01D,992,8B0,000
000.01D.992.8B0.000
000 01D 992 8B0 000
但我真正一直想得到的是这样的:
1D9928B0000
我想知道为什么会出现不同类型的格式以及如何防止进一步的格式意外以确保输出始终具有相同的格式。
我调用 to_hexadecimal()
函数的方式是这样的:
const auto memory_address = reinterpret_cast<PVOID>(0x1234567812345678);
const auto hexadecimal_memory_address = to_hexadecimal(memory_address);
std::cout << hexadecimal_memory_address << std::endl;
你的意图还不能完全理解。插入器操作符已经超载以打印出指针。如果你有一个指针,不管是哪个,你可以简单地将地址插入到流中:
#include <iostream>
int main() {
double d = 3.14;
// Print address in hex
std::cout << &d << '\n';
return 0;
}
您的函数不起作用,因为您还允许通过模板传递任何类型。
如果类型是普通双精度数,则 stream << value;
不会将其转换为十六进制。尤其不是地址。十六进制与否,没关系。根本没有转换。
不过如前所述。所有这些都不需要。请使用上面的方法。