Sign in to follow this  
Followers 0
decypher

Help with encoding

10 posts in this topic

This is what i'm trying to figure out...

$hFile = FileOpen("temp.txt", 128);

$cFile = FileRead($hfile);

I open temp.txt file (that contains UTF-8 string) as unicode UTF8 then I read it into $cFile.

The $cFile encoding is ascii now?! Then I try to StringSplit the content and send it

over http post method. The server gets ascii encoding in such case.

Should I encode it back to UTF-8 before post? looks a little strange...

first of all how do I encode it back to UTF-8? and won't I lose the characters

from such conversion?

Cannot figure out why the $cFile encoding is ascii after FileOpen 128 flag?

Thank you!

Share this post


Link to post
Share on other sites



After reading the file, your $cFile variable holds its contents. AutoIt use Unicode to store strings, so it's technically a UTF16-LE string.

As a matter of fact, it's generally impossible to perform a lossless conversion from Unicode to ANSI. Unicode encoding is irrelevant here.

Now how exactly do you post your string(s) is where the issue lies.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Now how exactly do you post your string(s) is where the issue lies.

before posting I do StringSplit content of the $cFile, then I use winhttp udf.

It looks as follow:

$text = StringSplit("+343+", $cFile, 1);
$hOpen = _WinHttpOpen();
$hConnect = _WinHttpConnect($hOpen, "example.local", 80);
$hRequest = _WinHttpOpenRequest($hConnect, "POST", "mcatapi/index.php", Default, "", "*/*");
$data = "mode=language&text=" & $text[1];
_WinHttpSendRequest($hRequest, "Content-Type: application/x-www-form-urlencoded" & @CRLF, $WINHTTP_NO_REQUEST_DATA, StringLen($data), 0);
_WinHttpWriteData($hRequest, $data);
_WinHttpReceiveResponse($hRequest);
ConsoleWrite(_WinHttpReadData($hRequest));
_WinHttpCloseHandle($hRequest);
_WinHttpCloseHandle($hConnect);
_WinHttpCloseHandle($hOpen);

Interesting thing here, I tried to post $text with text string I entered in autoit editor: $text = "some text in russian";

and i got right encoding at the server side which has en_US.UTF-8 locale.

Edited by decypher

Share this post


Link to post
Share on other sites

Look again at _WinHttpWriteData function header:

; #FUNCTION# ;===============================================================================
;
; Name...........: _WinHttpWriteData
; Description ...: Writes request data to an HTTP server.
; Syntax.........: _WinHttpWriteData($hRequest, $data [, $iMode])
; Parameters ....: $hRequest - Valid handle returned by _WinHttpSendRequest().
;               $data - Data to write.
;               [b]$iMode - Integer representing writing mode. Default is 0 - write ANSI string.[/b]
; Return values .: Success - Returns 1
;                       - @extended receives the number of bytes written.
;                       - Sets @error to 0
;               Failure - Returns 0 and sets @error:
;               |1 - DllCall failed.
; Author ........: trancexx, ProgAndy
; Modified.......:
; Remarks .......: $data variable is either string or binary data to write.
;               $iMode can have these values:
;               |0 - to write ANSI string
;               |1 - to write binary data
; Related .......:
; Link ..........; http://msdn.microsoft.com/en-us/library/aa384120(VS.85).aspx
; Example .......; Yes
;
;==========================================================================================
That means that the string is silently converted to ANSI (using your client locale) before being sent.

Now for the server locale: it uses UTF-8 encoding (meaning that it will gracefully process any UTF-8 encoded string) and other locale settings 'date/time, numbers, ... format as per en-US.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

That means that the string is silently converted to ANSI (using your client locale) before being sent.

Now for the server locale: it uses UTF-8 encoding (meaning that it will gracefully process any UTF-8 encoded string) and other locale settings 'date/time, numbers, ... format as per en-US.

i also tryed _WinHttpWriteData($hRequest, $data, 1);

with no result... the content was in ascii.

ps: there was a typo in my last code post: $text = StringSplit("+343+", $cFile, 1); -> $text = StringSplit($cFile, "+343+", 1);

Share this post


Link to post
Share on other sites

With the "binary" flag, the data is sent verbatim, that is as the string of bytes which code for UTF16-LE AUtoIt data. That isn't what you need either.

Try converting your AutoIt string to UTF-8 before sending it (using _WinAPI_WideCharToMultiByte with codepage = 65001)


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

With the "binary" flag, the data is sent verbatim, that is as the string of bytes which code for UTF16-LE AUtoIt data. That isn't what you need either.

Try converting your AutoIt string to UTF-8 before sending it (using _WinAPI_WideCharToMultiByte with codepage = 65001)

Wow! works now! :huh2: Thank you so much!!!!

Share this post


Link to post
Share on other sites

Fine!

Good luck for the rest.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

yet one question... How do I set utf-8 string (in russian)

to GUI (for example to _GuiCtrlListView control)?

Share this post


Link to post
Share on other sites

yet one question... How do I set utf-8 string (in russian)

to GUI (for example to _GuiCtrlListView control)?

found the solution :) _WinAPI_MultiByteToWideChar does the job

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0