development:cpp:char2string

C++ Converting unsigned char array to std::string

#include <stdio.h>
#include <iostream>
#include <fstream>
int  main()
{
        std::string badge = "/badges/";
        unsigned char badgechars[12];
        badgechars[0]=64; //demo
        badgechars[1]=72;
 
        std::string badge(reinterpret_cast<const char*>(badgechars),2);
        //or
        badge += std::string(badgechars, badgechars + 2);
        //or
        badge.append(1, badgechars[0]);
        badge.append(1, badgechars[1]);
 
        std::cout << "Searching for " << badge << std::endl;
        return 0;
}
Enter your comment:
C T​ M O F
 
  • development/cpp/char2string.txt
  • Last modified: 2019-10-31 09:04
  • (external edit)