How to calculate len size from unsigned char* in C++20?

6 days ago 10
ARTICLE AD BOX

Yes, unsigned char, signed char, and char are all exactly one byte large, so prores_ks_trellis_node_comp_glsl.size() gets you the size in bytes of your string.

The subsequent two lines are unnecessary, and it's also worth noting some problems:

std::u8string(prores_ks_trellis_node_comp_glsl.begin(), prores_ks_trellis_node_comp_glsl.end()).data() is taking the pointer to a temporary char8_t array, so that pointer immediately dangles. Also, calling the constructor of std::u8string like that doesn't do any transcoding. Your original string already needs to be UTF-8, otherwise this doesn't make much sense. There is no reason to use std::ssize instead of std::size here. You're trying to get a std::size_t result anyway, and std::size does that directly.

Assuming your goal is to "get the UTF-8 bytes" of the std::string, the proper way to write it looks like:

std::string s = "..."; std::span<const unsigned char> ut8_bytes( reinterpret_cast<const unsigned char*>(s.data()), s.size() );

This obviously assumes that your string literals are UTF-8 encoded though. If not, you'll need to transcode s to UTF-8 before taking the bytes.

Jan Schultke's user avatar

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Read Entire Article