将十六进制字符串转换为Crypto++中的字节
Convert Hex string to bytes in Crypto++
我有一个十六进制字符串,需要将其转换为const byte*
。我正在使用Crypto++进行哈希,它需要密钥在const byte*
中。有什么方法可以使用任何Crypto++库将十六进制字符串转换为const byte*
吗?或者我必须自己想出吗?
Crypto++中有一个HexDecoder
类。
您需要为这些字符添加内容。Crypto++似乎没有直接区分字符和字节。因此varren提供的以下代码行将起作用:
string destination;
StringSource ss(source, true, new HexDecoder(new StringSink(destination)));
const byte* result = (const byte*) destination.data();
我有一个十六进制字符串,需要将其转换为常量字节*
…
但它将是一串。我需要它在字节*中
那么您应该使用HexCoder和ArraySink。类似于:
string encoded = "FFEEDDCCBBAA99887766554433221100";
ASSERT(encoded.length() % 2 == 0);
size_t length = encoded.length() / 2;
unique_ptr<byte[]> decoded(new byte[length]);
StringSource ss(encoded, true /*pumpAll*/, new ArraySink(decoded.get(), length));
然后可以将字节数组decoded.get()
用作byte*
。
您也可以使用vector<byte>
。在这种情况下,byte*
是&v[0]
。类似于:
string encoded = "FFEEDDCCBBAA99887766554433221100";
ASSERT(encoded.length() % 2 == 0);
size_t length = encoded.length() / 2;
vector<byte> decoded;
decoded.resize(length);
StringSource ss(encoded, true /*pumpAll*/, new ArraySink(&decoded[0], length));
(评论)但它将是字符串。我需要它在字节*中
这更容易:
string encoded = "FFEEDDCCBBAA99887766554433221100";
string decoded;
StringSource ss(encoded, true /*pumpAll*/, new StringSink(decoded));
const byte* data = reinterpret_cast<const byte*>(decoded.data());
如果你想要非常量版本,那么使用:
byte* ptr = reinterpret_cast<byte*>(&decoded[0]);
// HEX to BIN using CryptoPP
string encoded = "FFEEDDCCBBAA99887766554433221100";
size_t length = encoded.length() / 2;
vector<byte> decoded;
decoded.resize(length);
StringSource ss(encoded, true, new HexDecoder(new ArraySink(&decoded[0], length)));
相关文章:
- 从不同线程使用int64的不同字节安全吗
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 在UNIX系统中使用DIR查找文件的字节大小
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- std::当在256字节边界上写入整数时,流的奇怪行为
- 当比特(而不是字节)的顺序至关重要时的持久性
- 从文件中读取多个字节,并将它们存储在C++中进行比较
- 如何在文件中查找字节序列
- luaL_dofile在已知良好的字节码上失败,可以使用未编译的版本
- 字节到位运算符重载C++
- 在java中读取c++字节的位字段
- 使用 std::vector::reverse_iterator 将 int 序列化为字节向量?
- 字节真的是最小可寻址单元吗
- struct.error:解压缩 C++ 结构时,解包需要 288 字节的缓冲区
- 读取文件中所有可能的十六进制 16 字节序列并打印每个序列
- 如何使用 OpenCV 解码在两个 UWP 应用之间发送的图像字节?
- crypto++将密钥保存到字节队列并返回到密钥
- 将十六进制字符串转换为Crypto++中的字节
- Crypto++多字节/unicode问题
- 如何在Crypto++中返回字节值