Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to use UTF8 characters in C++

How to use UTF8 characters in C++

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JohnCodding
    wrote on last edited by
    #1

    What do I need to do to be able to use in C++ UTF8 characters for char * and std::string, or at least for std::string if char * doesn't support it? I'm using std:c++17. I tried the following code to see if it is working without changing anything but it didn't worked and returned ┬┴╩╦≡??Γε?:

    #include
    #include

    int main() {
    std::string a = "ÂÁÊËðșțâîă";
    std::cout << a;
    std::cin.get();
    }

    Mircea NeacsuM L 2 Replies Last reply
    0
    • J JohnCodding

      What do I need to do to be able to use in C++ UTF8 characters for char * and std::string, or at least for std::string if char * doesn't support it? I'm using std:c++17. I tried the following code to see if it is working without changing anything but it didn't worked and returned ┬┴╩╦≡??Γε?:

      #include
      #include

      int main() {
      std::string a = "ÂÁÊËðșțâîă";
      std::cout << a;
      std::cin.get();
      }

      Mircea NeacsuM Offline
      Mircea NeacsuM Offline
      Mircea Neacsu
      wrote on last edited by
      #2

      Couple of things: 1. UTF8 literal strings are written as this:

      std::string a = u8"ÂÁÊËðșțâîă";

      2. Support for UTF8 in regular CMD is VERY bad. I haven't tried much in the new Windows terminal. 3. You might find more stuff about UTF8 in my series of articles on CodeProject.

      Mircea

      J 1 Reply Last reply
      0
      • J JohnCodding

        What do I need to do to be able to use in C++ UTF8 characters for char * and std::string, or at least for std::string if char * doesn't support it? I'm using std:c++17. I tried the following code to see if it is working without changing anything but it didn't worked and returned ┬┴╩╦≡??Γε?:

        #include
        #include

        int main() {
        std::string a = "ÂÁÊËðșțâîă";
        std::cout << a;
        std::cin.get();
        }

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You need to set the correct codepage to print the characters from the appropriate set. See chcp | Microsoft Docs[^]. Code page 65001 works for me.

        1 Reply Last reply
        0
        • Mircea NeacsuM Mircea Neacsu

          Couple of things: 1. UTF8 literal strings are written as this:

          std::string a = u8"ÂÁÊËðșțâîă";

          2. Support for UTF8 in regular CMD is VERY bad. I haven't tried much in the new Windows terminal. 3. You might find more stuff about UTF8 in my series of articles on CodeProject.

          Mircea

          J Offline
          J Offline
          JohnCodding
          wrote on last edited by
          #4

          Adding u8 worked, thanks. As for CMD it was used only as a testing display, I don't need it in the actual app, and for testing I ended up writing to a file to see if it is working. And where I actually used it, it did worked.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups