简体   繁体   中英

Copy a basic_string<char16_t> to vector<uint8_t>

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.

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 .

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