UTF-8 doc becomes ANSI doc !
-
Do the XML files contain something like this as their first line:
<?xml version="1.0" encoding="???" ?>
If so, can you provide that line to us?
If there is no such line, or if it does not include information about the encoding method, then UTF-8 is assumed.
I think that means that if the file contains a £ encoded as “A3” instead of “C2 A3”, that it isn’t technically valid XML (because it isn’t encoded as UTF-8).
Guy, Claudia, or anyone else who has a better understanding please correct me if I am wrong.
-
Jim, all the XMLs have
<?xml version=“1.0” encoding=“UFT-8” ?>
I am working on the source as well to see if there is anything can be done to save as UFT-8. I would like to approach the script option (Python), but at this time I do not have any idea how it is done. -
@Bahram-Yaghobinia
Sorry, I can’t help you with Python, but it seems like this part of the process:- A job picks up the data. Converts to xml and saves the xml in text file.
is broken because it does not (always) create valid XML. The XML is invalid any time it claims to be in UTF-8 but includes characters encoded in a single byte (e.g. a “£” encoded as 0xA3) that require multiple bytes to be properly encoded in UTF-8 (e.g. “£” should be encoded as 0xC2 0xA3).
-
I still think this is the wrong way to solve the problem because you loose the automatism by interacting
with npp to run the script, but you insist on the python script plugin solution so the lines in question arenotepad.runMenuCommand("Encoding", "Convert to UTF-8") notepad.save()
But this does only work if you have an english ui, in case you use a different language than you have to replace
“Encoding” and “Convert to UTF-8” with the ones from your language.Cheers
Claudia -
For me the problem is rather a problem of npp than anything.
The encoding of a text with no special character could be anything : iso-8859-1, iso-8859-2, iso-8859-15, windows-1252, utf-8, ASCII and I imagine a lot of others.
The encoding of a file with only normal characters can’t not be known just by looking at it !!!In php, there is a function : mb_detect_encoding($txt, array(encoding1, encoding2)).
When you use it, php try to know which encoding is used in the string $txt : it begins to look at the encoding1, if it fits the function answers encoding1, if it doesn’t the function try the second encoding and so and on.So if there is no special character in the $txt, the function will tell you the encoding is encoding1. In your case, with encoding1=utf-8, the function will tell you that the $txt is a utf-8 even if it’s impossible to know !
npp has no option to do the same (it is what I was looking for between my first post and my second one) except the “Settings - Preferences… - New Document - Apply to opened ANSI file” (thanks again Claudia).
You can’t obtain from npp that your docs are forever iso-5988-1, iso-5988-2 or ASCII ! For me it’s a big pb because a polish guy will probably use an ISO-5988-2 encoding and npp will tell some docs are in ANSI (that is windows-1252) and others are in ISO-5988-2 !So the pb is not only a pb with utf-8/ANSI but with a lot of encodings !
I think it will be a good evolution of npp to add an option to tell what to do when a doc is open and it is impossible to know the encoding : only the user can tell it, npp what clever it is will never !
-
you are correct, there is no way to always guess the correct encoding.
In regards to phps mb_detect_encoding function, npp is,
when “Autodetect character encoding” is checked, using mozillas chardet library,
so it has such functionality but, as you found already out, cannot guess the
correct encoding all time.I would also find it very useful if the setting
New Document->Encoding: UTF-8 and Apply to opened ANSI files (or any other configured encoding)
would force npp to treat all new opened documents as “configured encoding” when
auto detection of encoding has been disabled.Cheers
Claudia -
Let’s assume a file contains Byte-sequence 20-A9-20 (in ANSI this would be Space-Copyright-Space). This Sequence is invalid in UTF-8 so NPP has no alternative other than assuming an single-Byte encoding. And since it never does changes to the file’s content on its own, it is left to treat such a file as ANSI (or whatever your favorite single-Byte encoding is).
This is not a shortcoming of NPP but part of that single-Byte heritage we still have to deal with today.
-
I assume we have the same understanding so I’m interested to know
what I have written that could be misunderstood?
Could you point me to my error?Thank you and cheers
Claudia -
@Claudia-Frank said:
Not quite an error, butI would also find it very useful if the setting
New Document->Encoding: UTF-8 and Apply to opened ANSI files (or any other configured encoding)
would force npp to treat all new opened documents as “configured encoding” when
auto detection of encoding has been disabled.would require an implicit conversion to UTF-8. And besides breaking the principle of not doing changes without user action, it will pop up a whole bunch of other issues.
-
I agree that this would break the principle but on the other hand it could be beneficial as well.
But, now as I’m typing I’m thinking, when this conversion takes place and you don’t know from which encoding it came from
you might corrupt the document without knowing how to fix it.
Yes - bad idea.Cheers
Claudia