1

Please suggest an efficient way to copy bytes from a basic_string< char16_t> to vector< uint8_t>.

I am not concerned with the encoding, and just want to copy the bytes to the bytearray. It will later be interpreted with the correct encoding downstream.

Thanks.

SkypeMeSM
  • 3,197
  • 8
  • 43
  • 61

1 Answers1

4

An option is to get the data pointer, cast it to your new type and assign it to your target vector:

std::basic_string<char16_t> src;

const uint8_t *begin = reinterpret_cast<uint8_t const*>(src.data());
const uint8_t *end = reinterpret_cast<uint8_t const*>(src.data() + src.size());
std::vector<uint8_t> dst(begin,end);

This is one of the few cases where reinterpret_cast is a perfectly valid choice. From cppreference (highlights added by me):

Unlike static_cast, but like const_cast, the reinterpret_cast expression does not compile to any CPU instructions. It is purely a compiler directive which instructs the compiler to treat the sequence of bits (object representation) of expression as if it had the type new_type.

MatthiasB
  • 1,759
  • 8
  • 18
  • Thanks for your answer. I have done it that way. But I was actually wondering if there is another way avoiding reinterpret_casts. – SkypeMeSM Sep 04 '14 at 18:05
  • 1
    @SkypeMeSM I think in this case a reinterpret_cast is perfectly valid. After all, thats exactly what is done: an array of `char16_t` is bitwise interpreted as an array of `uint8_t`. – MatthiasB Sep 05 '14 at 06:51