C++:格式化包含十六进制值的字符串
C++: Format String Containing Hex Value
在Visual Studio 2010上使用C++。
试图想出一个健壮的函数,将十六进制值作为字符串,将大小作为整数,然后输出格式化的十六进制值。
例如,
如果输入字符串为"A2",大小为1,则输出为"0xA2">
如果输入字符串为"800",大小为2,则输出为"0x0800">
如果输入字符串为"DEF"且大小为4,则输出为"0x00000DEF">
如果输入字符串为"00775",大小为4,则输出为"0x00000775">
如果输入字符串为"FB600",大小为3,则输出为"0x0FB600">
其基本思想是,将大小乘以2,然后如果字符串长度小于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';
}
相关文章:
- 如何将包含epoch时间的十六进制字符串转换为time_t
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 指向存储在字符串 c++ 中的十六进制
- 如何在C++中将十六进制字符串转换为文本数据
- 使用 sprintf 将十六进制0xAABBCC转换为字符串"AA:BB:CC"
- 定义宏以将前缀 0x 添加到十六进制字符串文本
- 如何使用 setfill 和 setw 在字符串变量中存储十六进制值
- 将字符串(可以是十进制字符串或十六进制字符串)转换为整数C++
- 在Arduino中将字符串转换为(逗号分隔的十六进制)字符串到无符号字符数组
- 将 std::string( "x3BxDEx7C" ) 转换为可读的十六进制字符串?
- C++ 将十六进制字符串表示形式转换为十六进制字符串表示形式
- 字符数组到十六进制字符串的转换 - 意外输出
- C++将 wwn 字符串转换为识别为十六进制的数据类型
- 将字符串存储为十六进制而不进行转换
- C++:格式化包含十六进制值的字符串
- 字符串十六进制到指针获取值
- 如何从字符串十六进制中获取十进制
- Arduino:将字符串十六进制"#FFFFFF"转换为 3 int