How to Convert Unsigned Char* to Std::String in C++

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

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 ) ;

C++ conversion from unsigned char to std::string

To append a character you have to use the following overloaded method append

basic_string& append(size_type n, charT c);

For example

stroutput.append(1, characters[0]);
stroutput.append(1, characters[1]);

Otherwise use method push_back or the operator +=. For example

stroutput += characters[0];
stroutput += characters[1];

If you want to append the whole array or its part then you can write

stroutput.append( reinterpret_cast<char *>( characters ), sizeof( characters ) );

stroutput.append( reinterpret_cast<char *>( characters ), n );

where n is the number of characters of the array to append.

convert unsigned char* to std::string

std::string sName(reinterpret_cast<char*>(name));

reinterpret_cast<char*>(name) casts from unsigned char* to char* in an unsafe way but that's the one which should be used here. Then you call the ordinary constructor of std::string.

You could also do it C-style (not recommended):

std::string sName((char*) name);

const unsigned char * to std::string

You could try:

temp_doc.uuid = std::string(reinterpret_cast<const char*>(
sqlite3_column_text(this->stmts.read_documents, 0)
));

While std::string could have a constructor that takes const unsigned char*, apparently it does not.

Why not, then? You could have a look at this somewhat related question: Why do C++ streams use char instead of unsigned char?

Create std::string from std::span of unsigned char

You want:

auto ucharspan_to_stdstring(std::span<unsigned char const> input_array) -> std::string {
return std::string(input_array.begin(), input_array.end());
}

string, like other stand library containers, is constructible from an appropriate iterator pair - and this is such a pair. Since these are random access iterators, this will do a single allocation, etc.

Note that I changed from span<T> const& to span<T const>, for two reasons. First, you're not mutating the contents of the span, so the inner type needs to be const... similar to how you took a T const*, not a T*. Second, you should take spans by value because they're cheap to copy (unless you very specifically need the identity of the span, which you don't here).

It may be better to do a reinterpret_cast so that you can use the (char const*, size_t) constructor - this one ensures a single memcpy for the eventual write. But you'd have to time it to see if it's worthwhile.

How do I convert an unsigned char array into a string in C?

A string (char array) in C is a sequencial sequence of chars terminated by a sentianal character (the null terminator: '\0')

This means that if you have a byte of the value 0x00 anywhere in your array, it has thusly terminated the "string" potentially before the end of the sequence. In your example if you converted your data array into a string the end of it would be the first element:

data[]{00, EB, 23, EC, FF, etc... };

Now if you want to make a string out of the values of the data in here, you can do that with sprintf(), for example:

unsigned char val = 00;
data[0] = val;
char dump[5] = {0};

sprintf(dump, "%02x", data[0]);

Now you have a string with the value "00" in it, You can make it fancer with something like:

sprintf(dump, "0x%02x", data[0]);

So that it has "0x00" to show it off as hex data.

You could loop something like this to give the full string... but keep in mind that the printable string needs to be at least 2x+1 the size of the data string (2x for ASCII and +1 for null), and you have to fill the new string in steps. Example:

unsigned char data[5] = {0x00, 0x12, 0xB3, 0xFF, 0xF0};
char printstr[11] = {00};

for(int x = 0; x < 5; x++)
sprintf(printstr+(x*2), "%02x", data[x]);
printstr[10] = '\0';

Now printstr has "0012b3fff0"

converting unsigned char* to std::istream* C++

You can use an std::stringstream from <sstream>, which supports both istream and ostream interface. So you can write data through the ostream-interface and pass it then as an istream-argument:

#include <sstream>
#include <iomanip>
#include <iostream>

void prints(istream &is) {
unsigned char c;
while (is >> c) {
std::cout << "0x" << std::hex << (unsigned int)c << std::endl;
}
}

int main()
{
unsigned char x[6] = { 0x2, 0x10, 0xff, 0x0, 0x5, 0x8 };
std::stringstream xReadWrite;
xReadWrite.write((const char*)x, 6);
prints(xReadWrite);
}


Related Topics



Leave a reply



Submit