Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在visual c+中将字节数组转换为十六进制字符串+;?_C++_String_Visual C++_Hex - Fatal编程技术网

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

C++ 如何在visual c+中将字节数组转换为十六进制字符串+;?,c++,string,visual-c++,hex,C++,String,Visual C++,Hex,方法声明如下: //some.h void TDES_Decryption(BYTE *Data, BYTE *Key, BYTE *InitalVector, int Length); 我通过以下代码调用此方法: //some.c extern "C" __declspec(dllexport) bool _cdecl OnDecryption(LPCTSTR stringKSN, LPCTSTR BDK){ TDES_Decryption(m_Track1Buffer, m_cry

方法声明如下:

//some.h
void TDES_Decryption(BYTE *Data, BYTE *Key, BYTE *InitalVector, int Length);
我通过以下代码调用此方法:

//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;
}
其中,
m_Track1Buffer
的数据类型为
字节m_Track1Buffer[1000]

现在,我想对上述方法进行一些更改,即希望以十六进制返回
字符串,而不是
字节
。如何将此
m_Track1buffer
转换为
十六进制字符串
此代码将固定大小为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;
字节数组[100];
char-hexstr[201];
int i;

对于(i=0;i),正如您提到C++,这里是一个答案。Iomanip用于将十六进制形式的int存储到StrueSturn.<
#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();
}
#包括
#包括
字符串hexStr(字节*数据,整数长度)
{
std::stringstream-ss;

ss这里有一个更灵活的版本(使用大写字符?在字节之间插入空格?),可用于普通数组和各种标准容器:

#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();
}
用法示例(
std::vector
):

//用上面数组中的值填充
std::vector字节数组向量(std::begin(字节数组),std::end(字节数组));
auto from_vector=make_hex_string(byte_vector.begin()、byte_vector.end()、false);
断言(来自向量=“deadc0de00ff”);

像这样使用boost库如何(代码片段取自):

#包括
#包括
#包括
#包括
#包括
使用名称空间boost::算法;
int main()
{
向量v{'C','+','+'};
十六进制(v,std::ostream_迭代器{std::cout,“});

STD::CUT< P>使用代码> StrugSuth, SeaTFF和其他函数在循环中不是简单的C++。性能非常糟糕,这些函数通常被调用很多(除非你只是把一些东西写入日志)。 这里有一种方法。 不鼓励直接写入
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;
}
#包括
#包括
#包括
标准::字符串字节到十六进制字符串(常量标准::向量和输入)
{
静态常量字符[]=“0123456789ABCDEF”;
//不必要地将缓冲区归零,这对于std::string是无法避免的。
std::string ret(input.size()*2,0);
//违反规则,但避免复制整个缓冲区。
char*buf=const_cast(ret.data());
for(const auto&oneInputByte:input)
{
*buf++=字符[oneInputByte>>4];
*buf++=字符[oneInputByte&0x0F];
}
返回ret;
}
int main()
{
向量字节={341232520,11,52};

std::cout什么是
hex
string?你是指这里的
hex
string吗?请编辑并澄清。此外,给出输入和预期输出的示例总是很有帮助的。谢谢回答,但我应该如何返回它。我应该在这里返回什么变量?@AmitPal返回类似
std::string(hexstr)的内容
您需要使用
setfill('0')填充“0”字符
。我还必须添加std::setw,使其能够正确输出每个数字的2位数字。因此,现在看来
ss如果实现了注释,这个答案将是完美的。我不确定你是否因为Boost而被否决,但我认为你的答案是可以接受的,不应该有负面分数。对于Qt备选方案:
// 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");
#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';
}
#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;
}