Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Utf-8 encoding
Message
De
10/10/2020 07:29:21
 
 
À
09/10/2020 08:38:17
Information générale
Forum:
Visual FoxPro
Catégorie:
Autre
Titre:
Divers
Thread ID:
01676546
Message ID:
01676582
Vues:
30
Rick, thanks for your time and suggested solution. Interesting, no doubt at all.

>I need to create file with utf-8 encoding not ansi. File is created with low level functions fputs, fwrite. I can save as utf-8 with notepad or some other tools but I need it from prg. Remark is that is VFP6 so I can't use STRCONV(xx,9) from next version.
>
>Thanks in advance.

There's a Win32 function for converting UNICODE to UTF-8, and vice-versa.
CLEAR

* UTF-8 to Unicode
DECLARE INTEGER MultiByteToWideChar IN WIN32API ;
    INTEGER code_page, ;
    INTEGER flags, ;
    STRING mbString, INTEGER mbStringLength, ;
    STRING wcharString, INTEGER wcharStringLength

* Unicode to UTF-8
DECLARE INTEGER WideCharToMultiByte IN WIN32API ;
    INTEGER code_page, ;
    INTEGER flags, ;
    STRING wcharString, INTEGER wcharStringLength, ;
    STRING mbString, INTEGER mbStringLength, ;
    STRING defaultChar, ;
    INTEGER@ default_char_was_used

in = "test"
out = space(32)
in = MultiByteToWideChar(65001, 0, in, LEN(in), @out, LEN(out) / 2)
? ALLTRIM(out)

* Reverse it
in = ALLTRIM(ALLTRIM(out, 1, CHR(0)))
out = space(32)
llDefaultCharWasUsed = .f.
WideCharToMultiByte(65001, 0, in, LEN(in) / 2, @out, LEN(out), " ", @llDefaultCharWasUsed)
? ALLTRIM(out)
The UTF-8 file format has a leading BOM marker you'll need to write with FWRITE() first. This indicates to the file loader what format the file is in:
FWRITE(fh, CHR(0xEF) + CHR(0xBB) + CHR(0xB))
* Write the rest of your content after this.  Most apps will interpret straight-ansi as utf-8 without issue in my experience.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform