Converting "unsigned char *" to "char *" (and string)

call me Steve

I am working on a pet project with the libsodium library, and found that converting unsigned char * to char * was less straight forward than I imagined. Also I first was confused because the tests were passing in Release mode and only bit later I realised they were not passing in Debug mode. So I came up with the following:

std::string string_from_uchar(const unsigned char * c, unsigned long long lc)
    {
        unsigned char * cc = new unsigned char[lc+1] ;
        std::strncpy((char *) cc, (char *) c, lc);
        cc[lc] = 0;
        char* cr = reinterpret_cast<char *> (cc);
        std::string ret(cr);
        delete[](cr);
        return ret;
    }

While it now passed the tests, I was be grateful if someone could check if it is the right way of doing it (for instance would that work on another environment like gcc or clang?).

Lightness Races in Orbit

You're massively over-thinking this.

The copy is redundant, as is the extra dynamic allocation and the addition of a null terminator (because std::string has a constructor that accepts a length argument for just this sort of occasion).

The various char can be aliased, so simply:

std::string string_from_uchar(const unsigned char * c, unsigned long long lc)
{
    return std::string((const char*)c, lc);
}

In fact, if you use the constructor that takes a range, you don't even need the cast:

std::string string_from_uchar(const unsigned char * c, unsigned long long lc)
{
    return std::string(c, c + lc);
}

It barely even warrants being in its own function.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Converting a swift 4 string to c unsigned char *

Converting unsigned char * to hexstring

Converting unsigned char to int and short

Converting Eigen matrix to unsigned char *

Converting an unsigned char array to jstring

Converting unsigned char array to int

Swift String to unsigned char [](CUnsignedChar)

Convert hexadecimal string to unsigned char[]

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

Converting unsigned char array to signed short in C

Converting a float* or other pointer to unsigned char*

Defining a string as char* vs unsigned char*

How to copy a string to a unsigned char* on a struct?

Generating random string unsigned char in C

How to copy a std::string to unsigned char array?

Convert hex string into unsigned char in C++

Assigning a string variable to an unsigned char variable

Type conversion issues unsigned char, string, hex

In C11, string literals as char[], unsigned char[], char* and unsigned char*

Converting unsigned char pointer of data to struct containing ints

C: Converting unsigned char array to signed int (vice versa)

opencv c++: converting image matrix from unsigned char to float

Why is it ok to use a string literal to initialize an unsigned char array but not to initialize an unsigned char pointer?

Why is std::string implemented as basic_string of `char` and not `unsigned char`

Using unsigned char and char

How do you convert a `std::string` hex value to an `unsigned char`

Send unsigned char string from native client module to browser

A way to automatically cast string literals to `unsigned char*` in C?

How do I cast a slice of memoryview into C string (unsigned char*)?

TOP Ranking

HotTag

Archive