C++ now allows us to convert data types into bytes using std::byte.

​C++17 is a significant update for the language, and it brings a lot of features in the Standard Library. So far this course has covered the most important aspects, but there are many more things to describe!

std::byte is a small type that gives you a view of bytes and bits rather than numeric/char values (like unsigned char).

In the Standard it’s defined as enum:

enum class byte : unsigned char {} ; // in <cstddef>

You can initialize byte from unsigned char with the syntax byte b{unsigned char}, which is in fact, another handy C++17 feature that allows you to init a scoped enum with the underlying type[^enumunder].

To convert from byte into a numeric type use std::to_integer().

[^enumunder]: Read more in P0138 -

Let’s see a basic example:

Get hands-on with 1200+ tech skills courses.