如何在 Visual C++ 中将字节数组转换为十六进制字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14050452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert Byte Array to hex string in visual c++?
提问by Amit Pal
Declaration of a method are following:
方法声明如下:
//some.h
void TDES_Decryption(BYTE *Data, BYTE *Key, BYTE *InitalVector, int Length);
I am calling this method from the following code:
我从以下代码调用此方法:
//some.c
extern "C" __declspec(dllexport) bool _cdecl OnDecryption(LPCTSTR stringKSN, LPCTSTR BDK){
TDES_Decryption(m_Track1Buffer, m_cryptoKey, init_vector, len);
return m_Track1Buffer;
}
Where as data type of m_Track1Buffer
is BYTE m_Track1Buffer[1000];
Now i want to make some changes in above method i.e. want to return the String in hex
instead of Byte
. How should i convert this m_Track1buffer
to Hex string
其中数据类型m_Track1Buffer
是BYTE m_Track1Buffer[1000];
现在我想对上述方法进行一些更改,即要返回String in hex
而不是Byte
. 我应该如何将其转换m_Track1buffer
为Hex string
回答by 2r2w
As you have mentioned c++, here is an answer. Iomanip is used to store ints in hex form into stringstream.
正如您提到的 c++,这里是一个答案。Iomanip 用于将十六进制形式的整数存储到字符串流中。
#include <sstream>
#include <iomanip>
std::string hexStr(BYTE *data, int len)
{
std::stringstream ss;
ss << std::hex;
for( int i(0) ; i < len; ++i )
ss << std::setw(2) << std::setfill('0') << (int)data[i];
return ss.str();
}
回答by mvp
This code will convert byte array of fixed size 100 into hex string:
此代码将固定大小 100 的字节数组转换为十六进制字符串:
BYTE array[100];
char hexstr[201];
int i;
for (i=0; i<ARRAY_SIZE(array); i++) {
sprintf(hexstr+i*2, "%02x", array[i]);
}
hexstr[i*2] = 0;
回答by Max Truxa
Here is a somewhat more flexible version (Use uppercase characters? Insert spaces between bytes?) that can be used with plain arrays and various standard containers:
这是一个更灵活的版本(使用大写字符?在字节之间插入空格?),可以与普通数组和各种标准容器一起使用:
#include <string>
#include <sstream>
#include <iomanip>
template<typename TInputIter>
std::string make_hex_string(TInputIter first, TInputIter last, bool use_uppercase = true, bool insert_spaces = false)
{
std::ostringstream ss;
ss << std::hex << std::setfill('0');
if (use_uppercase)
ss << std::uppercase;
while (first != last)
{
ss << std::setw(2) << static_cast<int>(*first++);
if (insert_spaces && first != last)
ss << " ";
}
return ss.str();
}
Example usage (plain array):
示例用法(普通数组):
uint8_t byte_array[] = { 0xDE, 0xAD, 0xC0, 0xDE, 0x00, 0xFF };
auto from_array = make_hex_string(std::begin(byte_array), std::end(byte_array), true, true);
assert(from_array == "DE AD C0 DE 00 FF");
Example usage (std::vector
):
用法示例 ( std::vector
):
// fill with values from the array above
std::vector<uint8_t> byte_vector(std::begin(byte_array), std::end(byte_array));
auto from_vector = make_hex_string(byte_vector.begin(), byte_vector.end(), false);
assert(from_vector == "deadc0de00ff");
回答by serup
how about using the boost library like this (snippet taken from http://theboostcpplibraries.com/boost.algorithm):
像这样使用 boost 库怎么样(摘自http://theboostcpplibraries.com/boost.algorithm):
#include <boost/algorithm/hex.hpp>
#include <vector>
#include <string>
#include <iterator>
#include <iostream>
using namespace boost::algorithm;
int main()
{
std::vector<char> v{'C', '+', '+'};
hex(v, std::ostream_iterator<char>{std::cout, ""});
std::cout << '\n';
std::string s = "C++";
std::cout << hex(s) << '\n';
std::vector<char> w{'4', '3', '2', 'b', '2', 'b'};
unhex(w, std::ostream_iterator<char>{std::cout, ""});
std::cout << '\n';
std::string t = "432b2b";
std::cout << unhex(t) << '\n';
}
回答by BJovke
Using stringstream
, sprintf
and other functions in the loop is simply not C++. It's horrible for performance and these kind of functions usually get called a lot (unless you're just writing some things into the log).
在循环中使用stringstream
,sprintf
和其他函数根本不是 C++。这对性能来说很糟糕,而且这些类型的函数通常会被调用很多(除非你只是在日志中写一些东西)。
Here's one way of doing it.
Writing directly into the std::string
's buffer is discouraged because specific std::string implementation might behave differently and this will not work then but we're avoiding one copy of the whole buffer this way:
这是一种方法。std::string
不鼓励直接写入的缓冲区,因为特定的 std::string 实现可能会有不同的行为,这将不起作用,但我们通过这种方式避免了整个缓冲区的一个副本:
#include <iostream>
#include <string>
#include <vector>
std::string bytes_to_hex_string(const std::vector<uint8_t> &input)
{
static const char characters[] = "0123456789ABCDEF";
// Zeroes out the buffer unnecessarily, can't be avoided for std::string.
std::string ret(input.size() * 2, 0);
// Hack... Against the rules but avoids copying the whole buffer.
char *buf = const_cast<char *>(ret.data());
for (const auto &oneInputByte : input)
{
*buf++ = characters[oneInputByte >> 4];
*buf++ = characters[oneInputByte & 0x0F];
}
return ret;
}
int main()
{
std::vector<uint8_t> bytes = { 34, 123, 252, 0, 11, 52 };
std::cout << "Bytes to hex string: " << bytes_to_hex_string(bytes) << std::endl;
}