How does Windows Notepad interpret characters?
Asked Answered
B

5

12

I was wondering how Windows interprets characters.

I made a file with a hex editor with the 3 bytes E3 81 81.
Those bytes are the character in UTF-8.

I opened the notepad and it displayed . I didn't specify the encoding of the file, I just created the bytes and the notepad interpreted it correctly.

Is notepad somehow guessing the encoding?
Or is the hex editor saving those bytes with a specific encoding?

Brockway answered 20/7, 2011 at 22:28 Comment(0)
S
6

If the file only contains these three bytes, then there is no information at all about which encoding to use.

A byte is just a byte, and there is no way to include any encoding information in it. Besides, the hex editor doesn't even know that you intended to decode the data as text.

Notepad normally uses ANSI encoding, so if it reads the file as UTF-8 then it has to guess the encoding based on the data in the file.

If you save a file as UTF-8, Notepad will put the BOM (byte order mark) EF BB BF at the beginning of the file.

Snapdragon answered 20/7, 2011 at 22:44 Comment(2)
Yeah but i didnt save the file as UTF-8 or whatever, ive created the bytes. Thank youBrockway
@Guffa, This answer is wrong dude. He created the file with HxD. The file is only 3 bytes without BOM. Yet notepad didn't open it with ascii text, it does it in UTF8 text? How did notepad know this?Foolscap
D
3

Notepad makes an educated guess. I don't know the details, but loading the first few kilobytes and trying to convert them from UTF-8 is very simple, so it probably does something similar to that.

Diller answered 20/7, 2011 at 22:44 Comment(0)
I
2

...and sometimes it gets it wrong... https://ychittaranjan.wordpress.com/2006/06/20/buggy-notepad/

Importance answered 21/7, 2011 at 9:57 Comment(0)
N
2

There is an easy and efficient way to check whether a file is in UTF-8. See Wikipedia: http://en.wikipedia.org/w/index.php?title=UTF-8&oldid=581360767#Advantages, fourth bullet point. Notepad probably uses this.

Wikipedia claims that Notepad used the IsTextUnicode function, which checks whether a patricular text is written in UTF-16 (it may have stopped using it in Windows Vista, which fixed the "Bush hid the facts" bug): http://en.wikipedia.org/wiki/Bush_hid_the_facts.

Nitroso answered 13/11, 2013 at 17:22 Comment(1)
Bush hid the facts don't seem to work with me here... win 8.1. Yet the questioner's code works.Foolscap
T
0

how to identify the file is in which encoding ....?

Go to the file and try to Save As... and you can see the default (current) encoding of the file (in which encoding it is saved).

Telega answered 23/12, 2015 at 9:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.