How to convert UNICODE Hebrew appears as Gibberish in VBScript?

  1. I am gathering information from a HEBREW (WINDOWS-1255 / UTF-8 encoding) website using vbscript and WinHttp.WinHttpRequest.5.1 object.

For Example :

Set objWinHttp = CreateObject("WinHttp.WinHttpRequest.5.1")
...
'writes the file as unicode (can't use Ascii)
Set Fileout = FSO.CreateTextFile("c:\temp\myfile.xml", true, true) 

....
Fileout.WriteLine(objWinHttp.responsetext)
  1. When Viewing the file in notepad / notepad++, I see Hebrew as Gibrish / Gibberish. For example : äìëåú - äøá àáøäí éåñó - îåøùú

  2. I need a vbscript function to return Hebrew correctly, the function should be similar to the following http://www.pixiesoft.com/flip/ choosing the 2nd radio button and press convert button , you will see Hebrew correctly.

Upvotes: 4

Views: 8599

Answers (2)

Thanks to Charming Bobince (that posted the answer), I am now able to see HEBREW correctly (saving a windows-1255 encoding to a txt file (notpad)) by implementing the following :

Function ConvertFromUTF8(sIn)

        Dim oIn: Set oIn = CreateObject("ADODB.Stream")

        oIn.Open
        oIn.CharSet = "X-ANSI"
        oIn.WriteText sIn
        oIn.Position = 0
        oIn.CharSet = "WINDOWS-1255"
        ConvertFromUTF8 = oIn.ReadText
        oIn.Close

End Function

Upvotes: 2

bobince
bobince

Reputation: 536675

Your script is correctly fetching the byte stream and saving it as-is. No problems there.

Your problem is that the local text editor doesn't know that it's supposed to read the file as cp1255, so it tries the default on your machine of cp1252. You can't save the file locally as cp1252, so that Notepad will read it correctly, because cp1252 doesn't include any Hebrew characters.

What is ultimately going to be reading the file or byte stream, that will need to pick up the Hebrew correctly? If it does not support cp1255, you will need to find an encoding that is supported by that tool, and convert the cp1255 string to that encoding. Suggest you might try UTF-8 or UTF-16LE (the encoding Windows misleadingly calls 'Unicode'.)

Converting text between encodings in VBScript/JScript can be done as a side-effect of an ADODB stream. See the example in this answer.

Upvotes: 5

Related Questions