c++ - C++:格式化包含十六进制值的字符串
问题描述
在 Visual Studio 2010 上使用 C++。
试图提出一个强大的函数,它将十六进制值作为字符串和大小作为整数,然后输出格式化的十六进制值。
例如,
如果输入字符串为“A2”且大小为 1,则输出为“0xA2”
如果输入字符串为“800”且大小为 2,则输出为“0x0800”
如果输入字符串为“DEF”且大小为 4,则输出为“0x00000DEF”
如果输入字符串为“00775”且大小为4,则输出为“0x00000775”
如果输入字符串为“FB600”且大小为 3,则输出为“0x0FB600”
基本思想是,将大小乘以 2,然后如果字符串长度小于该长度,则将前导零添加到十六进制值,然后附加“0x”。
无论是否添加前导零,都会附加“0x”。
正如您在第一个示例中看到的,没有要添加的零,因为字符串已经包含 2 个字符。
我想出了下面的功能,但它有内存损坏。此外,当我尝试通过调用此函数数百次来处理大量数据时,它会崩溃。似乎我的逻辑中有记忆漏洞。
所以我希望有人能为这个功能想出一个健壮的智能代码。
我尝试了什么:
void formatHexString(char* inputHex, int size, char* outputFormattedHex)
{
int len = size * 2;
int diff = len - strlen(inputHex);
char * tempHex = new char [diff + 2]; //"2" is for holding "0x"
tempHex[0] = '0';
tempHex[1] = 'x';
if (len > strlen(inputHex))
{
for (int i = 2; i < ((len - strlen(inputHex)) + 2); i++)
{
tempHex[i] = '0';
}
}
strcat(tempHex, inputHex);
sprintf(outputFormattedHex, "%s", tempHex);
delete [] tempHex;
cout <<outputFormattedHex <<endl;
}
int main
{
char bbb1[24];
formatHexString("23", 1, bbb1);
char bbb2[24];
formatHexString("A3", 2, bbb2);
char bbb3[24];
formatHexString("0AA23", 4, bbb3);
char bbb4[24];
formatHexString("7723", 4, bbb4);
char bbb5[24];
formatHexString("AA023", 4, bbb5);
return 0;
}
更新:
我无法修改原始函数的参数,因为此函数是从不同的应用程序调用的。所以我用你的代码修改了我的原始函数,但这不起作用。有任何想法吗?
void formatHexString(char* inputHex, int size, char* outputFormattedHex)
{
string input(inputHex);
std::size_t const input_len(input.length());
if (!size || (size * 2 < input_len))
size = input_len / 2 + input_len % 2;
std::stringstream ss;
ss << "0x" << std::setw(2 * size) << std::setfill('0') << input;
sprintf(outputFormattedHex, "%s", ss.str());
}
解决方案
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <cstddef>
std::string formatHexString(std::string const & input, std::size_t size = 0)
{
std::size_t const input_len(input.length());
// always round up to an even count of digits if no size is specified
// or size would cause the output to be truncated
if (!size || (size * 2 < input_len))
size = input_len / 2 + input_len % 2;
std::stringstream ss;
ss << "0x" << std::setw(2 * size) << std::setfill('0') << input;
return ss.str();
}
int main()
{
std::cout << formatHexString( "23") << '\n'
<< formatHexString( "A3", 2) << '\n'
<< formatHexString( "AA23", 4) << '\n'
<< formatHexString( "7723", 4) << '\n'
<< formatHexString("AA023", 4) << '\n';
}
没有的解决方案std::stringstream
:
#include <string>
#include <cstddef>
std::string formatHexString(std::string const & input, std::size_t size = 0)
{
std::size_t const input_len(input.length());
// always round up to an even count of digits if no size is specified
// or size would cause the output to be truncated
if (!size || (size * 2 < input_len))
size = input_len / 2 + input_len % 2;
std::string result("0x");
for (std::size_t i = 0, leading_zeros = size * 2 - input_len; i < leading_zeros; ++i)
result += '0';
result += input;
return result;
}
更新:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
#include <cstddef>
#include <cstdio>
void formatHexString(char const * inputHex, int size, char * outputFormattedHex)
{
int const input_len(std::strlen(inputHex));
if (!size || (size * 2 < input_len))
size = input_len / 2 + input_len % 2;
std::stringstream ss;
ss << "0x" << std::setw(2 * size) << std::setfill('0') << inputHex;
std::strcpy(outputFormattedHex, ss.str().c_str());
}
int main()
{
char output[24];
formatHexString("23", 1, output);
std::cout << output << '\n';
formatHexString("A3", 2, output);
std::cout << output << '\n';
formatHexString("0AA23", 4, output);
std::cout << output << '\n';
formatHexString("7723", 4, output);
std::cout << output << '\n';
formatHexString("AA023", 4, output);
std::cout << output << '\n';
}
推荐阅读
- javascript - 成功登录尝试后重定向到另一个 html 页面
- mongodb - 在 MongoDB 中使用平面文档创建粒度
- reactjs - 与 async 和 await 一起使用时,Axios 似乎没有更新状态
- amazon-web-services - 为 DynamoDB template.yaml 使用 TableName 中的参数
- javascript - 基于子(非父)节点循环遍历对象数组
- android - 计时器问题
- reactjs - 如何在我的 FullStack React Express 应用程序中使用 Redux?
- java - 使用Java创建注册表并将其与在线数据库连接
- css - 顺风将一个 div 放在另一个 div 网格的底部
- excel - 提取保存公式的工作簿名称,而不是当前使用的工作簿名称