【问题标题】:How to convert Byte Array to Hexadecimal String in C++?如何在 C++ 中将字节数组转换为十六进制字符串?
【发布时间】:2012-04-11 05:14:03
【问题描述】:

我正在寻找一种将任意长度的字节数组转换为十六进制字符串的最快方法。这个问题已经完全回答了here at StackOverflow for C#。可以在here 找到一些 C++ 解决方案。

是否有针对问题的“交钥匙”或“现成”解决方案?欢迎使用 C 风格的解决方案。

【问题讨论】:

  • 您需要提供更多环境细节等,以决定实现这个非常简单的要求的“最快”方式。输出字符串是否已经分配?您有多少内存可用于查找表? “任意长度”很困难 - 对于非常大的数组,可能可以并行运行两个或多个转换。您的要求是否如此严格以至于您不能只使用您链接到的解决方案之一?

标签: c++ string hex byte


【解决方案1】:
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include <iterator>
#include <sstream>
#include <iomanip>

int main() 
{
  std::vector<unsigned char> v;

  v.push_back( 1 );
  v.push_back( 2 );
  v.push_back( 3 );
  v.push_back( 4 );

  std::ostringstream ss;

  ss << std::hex << std::uppercase << std::setfill( '0' );
  std::for_each( v.cbegin(), v.cend(), [&]( int c ) { ss << std::setw( 2 ) << c; } );

  std::string result = ss.str();

  std::cout << result << std::endl;
  return 0;
}

或者,如果您有一个支持统一初始化语法和基于范围的for 循环的编译器,您可以节省几行代码。

#include <vector>
#include <sstream>
#include <string>
#include <iostream>
#include <iomanip>

int main()
{
  std::vector<unsigned char> v { 1, 2, 3, 4 };
  std::ostringstream ss;

  ss << std::hex << std::uppercase << std::setfill( '0' );
  for( int c : v ) {
    ss << std::setw( 2 ) << c;
  }

  std::string result = ss.str();
  std::cout << result << std::endl;
}

【讨论】:

    【解决方案2】:

    使用boost::alogorithm::hex

    std::vector<unsigned char> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(4);
    std::string res;
    boost::algorithm::hex(v.begin(), v.end(), back_inserter(res));
    

    【讨论】:

      【解决方案3】:

      您可以使用 C++ 标准库,也可以使用 boost::lexical_cast

      #include <iostream>
      #include <string>
      #include <array>
      #include <vector>
      #include <sstream>
      #include <iomanip>
      #include <algorithm>
      
      using namespace std;
      
      // use this macro for c++11 feature
      #define USE_CPP11
      
      int main(int argc, char* argv[])
      {
          array<unsigned char, 3> hexArr = {0x01, 0xff, 0x55};
          const char separator = ' ';             // separator between two numbers
          ostringstream os;
          os << hex << setfill('0');  // set the stream to hex with 0 fill
      
      #ifdef USE_CPP11
          std::for_each(std::begin(hexArr), std::end(hexArr), [&os, &separator] (int i)
          {
              os << setw(2) << i << separator;
          });
      #else       // c++03
          typedef array<unsigned char, 3>::const_iterator const_iterator;
          for (const_iterator it = hexArr.begin(); it != hexArr.end(); ++it)
          {
              os << setw(2) << int(*it) << separator;
          }
      #endif
          os << dec << setfill(' ');          // reset the stream to "original"
      
          // print the string
          cout << "the string array is: " << os.str() << endl;
      
          return EXIT_SUCCESS;
      }
      

      【讨论】:

        【解决方案4】:

        我知道的 C++ 11 中最快的方法之一:

        template <size_t byteCount>
        string BytesArrayToHexString( const std::array<byte, byteCount>& src )
        {
          static const char table[] = "0123456789ABCDEF";
          std::array<char, 2 * byteCount + 1> dst;
          const byte* srcPtr = &src[0];
          char* dstPtr = &dst[0];
        
          for (auto count = byteCount; count > 0; --count)
          {
              unsigned char c = *srcPtr++;
              *dstPtr++ = table[c >> 4];
              *dstPtr++ = table[c & 0x0f];
          }
          *dstPtr = 0;
          return &dst[0];
        }
        

        一个好的编译器在这个上应用 SSE 优化应该没有任何问题......

        【讨论】:

          猜你喜欢
          • 2017-06-17
          • 1970-01-01
          • 1970-01-01
          • 2011-09-15
          • 2019-03-07
          • 2013-01-14
          • 2017-08-23
          • 2011-01-10
          • 2015-09-21
          相关资源
          最近更新 更多