Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2016-10

How to know the encoding of a string?

7 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Marcos Douglas B. Santos

Hi,

Is there a way to know what encoding was used at a string?

My system are receiving some files and I don't know the encoding. I would like to know and then, convert it.

Best regards,
Marcos Douglas

Jonas Maebe
to Marcos Douglas B. Santos
Marcos Douglas B. Santos wrote
Is there a way to know what encoding was used at a string? My system are receiving some files and I don't know the encoding. I would like to know and then, convert it.

There are some heuristics you could try to apply (see e.g. http://chsdet.sourceforge.net ), but in general it is impossible to know for sure what encoding has been used.

Jonas

Graeme Geldenhuys
to Marcos Douglas B. Santos: Is there a way to know what encoding was used at a string? My system are receiving some…
Marcos Douglas B. Santos wrote
My system are receiving some files and I don't know the encoding. I would like to know and then, convert it.

No such function [with exact results] exists anywhere (no matter the programming language). The best programs can do is to interrogate the contents of a file and make a best guess. How they do that interrogation -- I unfortunately don't know. I guess you could look at the source code of some of the open source text editors and see what they do. Some text editors are much better than others. eg: EditPad Pro (commercial) and jEdit (open source) are very good at these interrogations.

Alternatively, simply ask the person that supplies you with these files to tell you want encoding they use. They must know the answer.

Regards,
Graeme

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp
Bart
to Marcos Douglas B. Santos: Is there a way to know what encoding was used at a string? My system are receiving some…
Marcos Douglas B. Santos wrote
Is there a way to know what encoding was used at a string? My system are receiving some files and I don't know the encoding. I would like to know and then, convert it.

As explained there is no certain way.
Lazarus comes with lconvencoding unit which has a guessencoding function. Maybe this can be of some help to you?

(Ruling out a string is UTF8 (compatible) can be done 100%)

Bart

Marcos Douglas B. Santos
to Graeme Geldenhuys: No such function [with exact results] exists anywhere (no matter the programming…
Graeme Geldenhuys wrote 7 lines
No such function [with exact results] exists anywhere (no matter the programming language). The best programs can do is to interrogate the contents of a file and make a best guess. How they do that interrogation -- I unfortunately don't know. I guess you could look at the source code of some of the open source text editors and see what they do. Some text editors are much better than others. eg: EditPad Pro (commercial) and jEdit (open source) are very good at these interrogations.

Thanks. I'll try http://chsdet.sourceforge.net (Jonas' tip).

Alternatively, simply ask the person that supplies you with these files to tell you want encoding they use. They must know the answer.

It's not so simple... :)

Marcos Douglas