简体   繁体   中英

Can I get a char* out of either signed char* or unsigned char*?

I have to deal with char arrays which might be unsigned (because they come from a SCSI data block). I wanted to handle them with this function:

template <typename CharT, size_t Len>
std::string strFromArray(CharT (&src) [Len])
{     
    return std::string((typename boost::make_signed<CharT>::type *) src, Len);
}

The error is in the std::string constructor call, it cannot take signed char / unsigned char but will only take char .

I could of course replace the cast with (char *) src , but then I would lose all compiler errors if I pass in a non-char type.

How can I write this so that it constructs strings out of all "charry" types?

Assuming that the conversion to string indicates that the byte arrays carry null-terminated C strings (eg string literals):

#include <string>
#include <stddef.h>

namespace my {
    using std::string;
    using Size = ptrdiff_t;

    namespace detail {
        auto is_char_type( char const* ) -> bool;
        auto is_char_type( signed char const* ) -> bool;
        auto is_char_type( unsigned char const* ) -> bool;
    }  // namespace detail

    template< class Char, Size n >
    auto string_from( Char (&src) [n] )
        -> string
    {     
        (void) sizeof( detail::is_char_type( src ) );
        return string( src, src + n - 1 );
    }
}  // namespace my

auto main() -> int
{
    unsigned char const data[] = "Blah";
    auto const s = my::string_from( data );

#ifdef TEST_WCHAR
    wchar_t const wdata[] = L"Blah";
    auto const ungood_s = my::string_from( wdata );      // Doesn't compile.
#endif
}

If instead of strings this is about arbitrary binary data, then use just src + n instead of src + n - 1 .

However, in the case of binary data, there is a possibility that you need a separate length, ie not using the length of the raw array itself.

I would keep it as simple as possible, and use:

#include <iostream>
#include <string>

namespace conv {
    template <size_t len>
    std::string strFromArray(const char(&arr)[len])
    {
        static_assert(len > 0, "don't use zero-sized arrays");
        return std::string(arr, len - 1);
    }

    template <size_t len>
    std::string strFromArray(const unsigned char(&arr)[len])
    {
        static_assert(len > 0, "don't use zero-sized arrays");
        return std::string((const char *)arr, len - 1);
    }
}

int main()
{
    const char charstr[] = "abcd";
    std::string str = conv::strFromArray(charstr);
    std::cout << str << std::endl;

    const unsigned char ucharstr[] = "efg";
    str = conv::strFromArray(ucharstr);
    std::cout << str << std::endl;

/*not possible:
    const wchar_t wcharstr[] = L"hijk";
    str = conv::strFromArray(wcharstr);
*/

}

Test it live

Just place an static assert into your function and modify it slightly:

#include <string>

template <typename CharT, std::size_t Len>
std::string strFromArray(CharT (&src) [Len])
{
    // Anythig which looks like a char is considered a char.
    static_assert(sizeof(CharT) == sizeof(char), "Invalid Character Type");
    std::size_t n = Len;
    // Do not use a terminating zero (might be wrong if the source is no char 
    // literal, but an array of binary data.
    if( ! src[n-1])
        --n;
    return std::string(src, src + n);
}

int main()
{
    char c[3] = {};
    signed char sc[3] = {};
    unsigned char uc[3] = {};
    wchar_t wc[3] = {};
    strFromArray(c);
    strFromArray(sc);
    strFromArray(uc);
    // error: static assertion failed: Invalid Character Type
    // strFromArray(wc);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM