如何在Visual c ++中将字节数组转换为十六进制字符串?

时间:2022-09-01 12:04:45

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];现在我想在上面的方法中进行一些更改,即想要以十六进制而不是Byte返回String。我应该如何将此m_Track1buffer转换为十六进制字符串

4 个解决方案

#1


11  

As you have mentioned c++, here is an answer. Iomanip is used to store ints in hex form into stringstream.

正如你提到的c ++,这是一个答案。 Iomanip用于将十六进制形式的整数存储到stringstream中。

#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<<(int)data[i];
    return ss.str();
}

#2


9  

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;

#3


7  

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");

#4


-1  

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';
}

#1


11  

As you have mentioned c++, here is an answer. Iomanip is used to store ints in hex form into stringstream.

正如你提到的c ++,这是一个答案。 Iomanip用于将十六进制形式的整数存储到stringstream中。

#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<<(int)data[i];
    return ss.str();
}

#2


9  

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;

#3


7  

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");

#4


-1  

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';
}