编译时将字符*转换为字节
compile-time conversion char* to bytes
我有很多从常量字符串(在编译时已知(到字节字符串的转换。 现在我使用这个函数来做:
std::string StrToHex(std::string);
"0f120a" -> {0x0f, 0x12, 0x0a};
但这需要很多时间,我想在编译时生成所有这些字符串。
从常量字符串(在编译时已知(到字节字符串的转换
为了证明(字符串的(数据已经是一个字节字符串, 请考虑以下代码
#include <iostream>
using std::cout, std::hex, std::dec;
#include <string>
using std::string;
#include <iomanip>
using std::setw;
// s1 does NOT change
const string s1 = "0123456789Ab";
cout << "n letters: ";
for (uint i=0; i<s1.size(); ++i)
{
cout << " " << setw(4) << s1[i];
}
cout << "n hex: " << hex; // format info
for (uint i=0; i<s1.size(); ++i)
{
cout << " " << setw(4) << static_cast<int>(s1[i]);
} // // ^^^^^^^^^^^ use the char as an int
cout << "n decimal: " << dec;
for (uint i=0; i<s1.size(); ++i)
{
cout << " " << setw(4) << static_cast<int>(s1[i]);
} // // ^^^^^^^^^^^ use the char as an int
cout << "n letters: (confirm s1 did not change)";
for (uint i=0; i<s1.size(); ++i)
{
cout << " " << setw(4) << s1[i];
}
输出。。。字符串的字节可以打印为字符、十六进制整数或十进制整数。
letters: 0 1 2 3 4 5 6 7 8 9 A b
hex: 30 31 32 33 34 35 36 37 38 39 41 62
decimal: 48 49 50 51 52 53 54 55 56 57 65 98
letters: 0 1 2 3 4 5 6 7 8 9 A b
相关文章:
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 将字节数组转换为带有字节序问题的指针
- 使用 stbi_write_png,如何将 0 和 1 的矩形字节数组转换为单色 png 文件?
- 如何在连接器 C++ 中将原始字节转换为字符串
- 如何在C++(Arduino)中将浮点数组转换为字节数组
- 将字节数组转换为多维数组
- C++如何将字符数组转换为字节向量(向量<byte>)
- 如何在C++中将字节向量(std::vector<uint8_t>)转换为不同的uint32_t,uint16_t和uint8_t变量
- 将字符串转换为无符号字符数组/字节数组
- 将(N 个字节)无符号字符指针转换为浮点数和双 C++
- 将以 null 结尾的字节字符串转换为原始字符串文本
- 正在转换结构数据的字节序
- 如何将 CString 转换为字节 [] 数组?
- 一种将 Dart 中的字节数据转换为 C++ 中的无符号字符*的有效方法?
- 使用 ESP8266/8285 将字符串从 Web 服务器转换为字节数组
- 编译时将字符*转换为字节
- 将字节数组转换为对象C++ |C++中的 C# 位转换器
- C++转换字节
- 将整型转换字节中的字节更改为整数
- c++从字符串化字节转换字节数组