Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

char32_t and char16_t

Use case

Handle Unicode strings in binary resources (Windows PE uses UTF-16).

Explanation

char16_t and char32_t are fixed-width Unicode types. Use u"..." for UTF-16 and U"..." for UTF-32.

Code

#include <cstdint>
#include <iostream>

int main() {
  char16_t utf16[] = u"MZ";
  (void)utf16;
  char32_t utf32[] = U"MZ";
  (void)utf32;

  std::cout << "UTF-16 element size: " << sizeof(char16_t) << " bytes.\n";
  std::cout << "UTF-32 element size: " << sizeof(char32_t) << " bytes.\n";

  return 0;
}

View on GitHub.

Output

$ ./src/c++11/build/char32_t-and-char16_t
UTF-16 element size: 2 bytes.
UTF-32 element size: 4 bytes.