• Login
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.
  • X
    xb zhou
    last edited by Nov 27, 2023, 7:35 AM

    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!

    C 1 Reply Last reply Nov 27, 2023, 5:34 PM Reply Quote 1
    • G
      gstavi
      last edited by Nov 27, 2023, 9:33 AM

      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.

      X 1 Reply Last reply Nov 30, 2023, 9:28 AM Reply Quote 4
      • C
        Coises @xb zhou
        last edited by Nov 27, 2023, 5:34 PM

        @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.

        A X 2 Replies Last reply Nov 27, 2023, 5:43 PM Reply Quote 4
        • A
          Alan Kilborn @Coises
          last edited by Nov 27, 2023, 5:43 PM

          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.

          X 1 Reply Last reply Nov 30, 2023, 9:34 AM Reply Quote 3
          • X
            xb zhou @gstavi
            last edited by Nov 30, 2023, 9:28 AM

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

            1 Reply Last reply Reply Quote 1
            • X
              xb zhou @Coises
              last edited by Nov 30, 2023, 9:32 AM

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

              1 Reply Last reply Reply Quote 0
              • X
                xb zhou @Alan Kilborn
                last edited by Nov 30, 2023, 9:34 AM

                @Alan-Kilborn Thank you!

                1 Reply Last reply Reply Quote 0
                • A Alan Kilborn referenced this topic on Feb 2, 2024, 12:42 PM
                5 out of 7
                • First post
                  5/7
                  Last post
                The Community of users of the Notepad++ text editor.
                Powered by NodeBB | Contributors