Jump to content

BinaryToString() gives garbled text returned from InetRead()


kcston
 Share

Recommended Posts

I’m writing a simple Autoit script to translate text from Japanese to English using Google Translate API key

 

If I enter the following directly into a browser, it works correctly. (Actual API key not included here)
    
https://translation.googleapis.com/language/translate/v2?key=MyGoogleTranslateAPIkey&source=ja&target=en&q=モーションシステム
Result:

{
  "data": {
    "translations": [
      {
        "translatedText": "motion system"
      }
    ]
  }
}

 

However, if I do it using the following code (Autoit v3), I get garbled text instead.

Dim $Target

$Target= InetRead ("https://translation.googleapis.com/language/translate/v2?key=MyGoogleTranslateAPIkey&source=ja&target=en&q=モーションシステム")
$Target = BinaryToString ($Target, 4)
ConsoleWrite($Target)

Result:

{
  "data": {
    "translations": [
      {
        "translatedText": "[ VVX e"
      }
    ]
  }
}

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

What is the console output of the following script?

Local $Target= InetRead ("https://translation.googleapis.com/language/translate/v2?key=MyGoogleTranslateAPIkey&source=ja&target=en&q=モーションシステム")
ConsoleWrite($Target & @LF)

 

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)

Link to comment
Share on other sites

48 minutes ago, jchd said:

What is the console output of the following script?

Local $Target= InetRead ("https://translation.googleapis.com/language/translate/v2?key=MyGoogleTranslateAPIkey&source=ja&target=en&q=モーションシステム")
ConsoleWrite($Target & @LF)

 

 

Thanks for the quick response, jchd. Here's the result using 

ConsoleWrite($Target & @LF)

 

Apparently, there is no change.

 

{
  "data": {
    "translations": [
      {
        "translatedText": "[ VVX e"
      }
    ]
  }
}

 

 

Link to comment
Share on other sites

Sorry, I did not know how to delete the just posted reply. Here's what ConsoleWrite() returned:

0x7B0A20202264617461223A207B0A20202020227472616E736C6174696F6E73223A205B0A2020202020207B0A2020202020202020227472616E736C6174656454657874223A20225B205656582065220A2020202020207D0A202020205D0A20207D0A7D0A

 

Link to comment
Share on other sites

Then the result received isn't binary, or @error tells you things didn't work well. What is @error after InetRead?

Also try setting $INET_BINARYTRANSFER + $INET_FORCEBYPASS in InetRead invokation.

If that still doesn't work, maybe the API is expecting UTF8 data, which your browser uses internally.

Edited by jchd

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)

Link to comment
Share on other sites

Try converting the request string to UTF8 before sending it.

Edited by jchd

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)

Link to comment
Share on other sites

7 hours ago, jchd said:

Try converting the request string to UTF8 before sending it.

You are right, jchd. That seems to be the problem. If I paste the whole URL including the Japanese text into Edge, it automatically converts the Japanese text into UTF8 (?).  If I then copy it into InetRead(), it works and returns the correct result.

So, I need to convert text to UTF8 first before putting it into InetRead().

Is _WinAPI_WideCharToMultiByte() the function to do the job? It seems not to be an Autoit built-in function but requires <WinAPIConv.au3> but I could not find the file despite all the efforts. 

 

Link to comment
Share on other sites

Easy to do:

Local $sUCS2 = "モーションシステム"
Local $sTUF8 = BinaryToString(StringToBinary($sUCS2, 4), 1)

 

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)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...