Community
    • Login

    How can I get the encoding of current document?

    Scheduled Pinned Locked Moved Notepad++ & Plugin Development
    7 Posts 4 Posters 2.5k Views
    Loading More Posts
    • 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.
    • xb zhouX
      xb zhou
      last edited by

      Hello, I want to get the encoding of current document, such as UTF8、ANSI、Chinese GB2312。

      I use code below:

      int32_t buffer_id = ::SendMessage(nppData._nppHandle, NPPM_GETCURRENTBUFFERID, 0, 0);
      int32_t encoding = ::SendMessage(nppData._nppHandle, NPPM_GETBUFFERENCODING, buffer_id, 0);

      The encoding return 0, when the document’s encoding is ANSI(show in Encoding menu of Notepad++),But Both return 4 when the document’s encoding is UTF8 and Chinese GB2312.

      So, I can’t distinguish UTF8 and GB2312. What’s wrong? Thanks a lot!

      CoisesC 1 Reply Last reply Reply Quote 1
      • gstaviG
        gstavi
        last edited by

        First, is this relevant?
        Getting encoding in x64 plugin

        Second, make sure you distinguish encoding from codepage.
        Perhaps you need also Scintilla SCI_GETCODEPAGE message.

        xb zhouX 1 Reply Last reply Reply Quote 4
        • CoisesC
          Coises @xb zhou
          last edited by

          @xb-zhou said in How can I get the encoding of current document?:

          So, I can’t distinguish UTF8 and GB2312. What’s wrong?

          Per documentation, NPPM_GETBUFFERENCODING returns a UniMode enum, which is defined here.

          Notepad++ doesn’t handle character sets internally the way it appears to a user. For editing, everything is either in the user default code page (“ANSI”) or in UTF-8. Whenever you’re not using the default code page, Notepad++ uses UTF-8 (so it is possible to enter and see characters that aren’t in the code page). Translation to other code pages is done when reading and writing the file. I don’t know the details beyond that; if no one else responds with a deeper explanation, you’ll probably need to read the code to figure out what will work for your particular use case.

          Alan KilbornA xb zhouX 2 Replies Last reply Reply Quote 4
          • Alan KilbornA
            Alan Kilborn @Coises
            last edited by

            THIS THREAD got off into a tangent about encoding issues; might want to have a look there to round out some understanding about how it all works.

            xb zhouX 1 Reply Last reply Reply Quote 3
            • xb zhouX
              xb zhou @gstavi
              last edited by

              @gstavi Yes,SCI_GETCODEPAGE message is what I find. Thank you verymuch.

              1 Reply Last reply Reply Quote 1
              • xb zhouX
                xb zhou @Coises
                last edited by

                @Coises Thank you for your answer! It was very valuable.

                1 Reply Last reply Reply Quote 0
                • xb zhouX
                  xb zhou @Alan Kilborn
                  last edited by

                  @Alan-Kilborn Thank you!

                  1 Reply Last reply Reply Quote 0
                  • Alan KilbornA Alan Kilborn referenced this topic on
                  • First post
                    Last post
                  The Community of users of the Notepad++ text editor.
                  Powered by NodeBB | Contributors