C++ 如何在C++;?

C++ 如何在C++;?,c++,string,hex,byte,C++,String,Hex,Byte,我正在寻找一种将任意长度的字节数组转换为十六进制字符串的最快方法。这个问题已经完全回答了。可以找到C++中的一些解决方案。p> 是否有任何“交钥匙”或“现成”的问题解决方案?欢迎使用C样式的解决方案。 < P>可以使用C++标准库,或者可以使用Booo::ListalyOxCase #include <vector> #include <iostream> #include <algorithm> #include <string> #includ

我正在寻找一种将任意长度的字节数组转换为十六进制字符串的最快方法。这个问题已经完全回答了。可以找到C++中的一些解决方案。p>
是否有任何“交钥匙”或“现成”的问题解决方案?欢迎使用C样式的解决方案。

< P>可以使用C++标准库,或者可以使用Booo::ListalyOxCase

#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;
}
#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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//将此宏用于c++11功能
#定义使用\u CPP11
int main(int argc,char*argv[])
{
数组hexArr={0x01,0xff,0x55};
常量字符分隔符=“”;//两个数字之间的分隔符
ostringstream os;
操作系统使用

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));
std::vector v;
v、 推回(1);
v、 推回(2);
v、 推回(3);
v、 推回(4);
std::string res;
boost::algorithm::hex(v.begin(),v.end(),back_inserter(res));

<代码> > p> >我在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];
}
模板
字符串BytesArrayToHexString(常量std::数组和src)
{
静态常量字符表[]=“0123456789ABCDEF”;
std::阵列dst;
常量字节*srcPtr=&src[0];
char*dstPtr=&dst[0];
对于(自动计数=字节计数;计数>0;--count)
{
无符号字符c=*srcPtr++;
*dstPtr++=表[c>>4];
*dstPtr++=表[c&0x0f];
}
*dstPtr=0;
返回&dst[0];
}

一个好的编译器在这方面应用SSE优化应该不会有任何问题。…

您需要提供更多的环境细节等,以决定实现这一非常简单要求的“最快”方式。输出字符串是否已分配?您有多少内存可用于查找表?“任意长度”很难-w对于非常大的阵列,可以并行运行两个或多个转换。您的要求是否严格到不能只使用链接到的解决方案之一?
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];
}