How to Convert unsigned char* to std::string in C++?

前端 未结 6 1279
予麋鹿
予麋鹿 2020-11-29 02:56

I have unsigned char*, want to convert it to std::string. Can you please tell me the safest way to do this?

相关标签:
6条回答
  • 2020-11-29 03:18

    BYTE is nothing but typedef unsigned char BYTE;

    You can easily use any of below constructors

    string ( const char * s, size_t n );
    string ( const char * s );
    
    0 讨论(0)
  • 2020-11-29 03:19

    You just needed to cast the unsigned char into a char as the string class doesn't have a constructor that accepts unsigned char:

    unsigned char* uc;
    std::string s( reinterpret_cast< char const* >(uc) ) ;
    

    However, you will need to use the length argument in the constructor if your byte array contains nulls, as if you don't, only part of the array will end up in the string (the array up to the first null)

    size_t len;
    unsigned char* uc;
    std::string s( reinterpret_cast<char const*>(uc), len ) ;
    
    0 讨论(0)
  • 2020-11-29 03:22

    BYTE* is probably a typedef for unsigned char*, but I can't say for sure. It would help if you tell us what BYTE is.

    If BYTE* is unsigned char*, you can convert it to an std::string using the std::string range constructor, which will take two generic Iterators.

    const BYTE* str1 = reinterpret_cast<const BYTE*> ("Hello World");
    int len = strlen(reinterpret_cast<const char*>(str1));
    std::string str2(str1, str1 + len);
    

    That being said, are you sure this is a good idea? If BYTE is unsigned char it may contain non-ASCII characters, which can include NULLs. This will make strlen give an incorrect length.

    0 讨论(0)
  • 2020-11-29 03:38

    If has access to CryptoPP

    Readable Hex String to unsigned char

    std::string& hexed = "C23412341324AB";
    uint8_t      buffer[64] = {0};
    StringSource ssk(hexed, true,
                new HexDecoder(new ArraySink(buffer,sizeof(buffer))));
    

    And back

    std::string hexed;
    uint8_t val[32]  = {0};
    StringSource ss(val, sizeof(val), true,new HexEncoder(new StringSink(hexed));
    // val == buffer
    
    0 讨论(0)
  • 2020-11-29 03:41
    BYTE *str1 = "Hello World";
    std::string str2((char *)str1);  /* construct on the stack */
    

    Alternatively:

    std::string *str3 = new std::string((char *)str1); /* construct on the heap */
    cout << &str3;
    delete str3;
    
    0 讨论(0)
  • 2020-11-29 03:42

    Here is the complete code

    #include <bits/stdc++.h>
    
    using namespace std;
    
    typedef unsigned char BYTE;
    
    int main() {
      //method 1;
      std::vector<BYTE> data = {'H','E','L','L','O','1','2','3'};
      //string constructor accepts only const char
      std::string s((const char*)&(data[0]), data.size());
      std::cout << s << std::endl;
    
      //method 2
      std::string s2(data.begin(),data.end());
      std::cout << s2 << std::endl;
    
      //method 3
      std::string s3(reinterpret_cast<char const*>(&data[0]), data.size()) ;
      std::cout << s3 << std::endl;
     
      return 0;
    }
    
    0 讨论(0)
提交回复
热议问题