Jump to content
Danp2

Remove Unicode (BOM) from beginning of string?

Recommended Posts

Hi all,

I'm reading the file https://msedgedriver.azureedge.net/LATEST_RELEASE_83 using WinHTTP, and the string I get back looks like this when I view this file in Scite --

xEFxBFxBF83.0.478.54

Here's what I've come up with as a means to strip off the Unicode BOM from the beginning of the string --

If Asc($sDriverLatest) = 255 Then
    ConsoleWrite("Unicode detected!" & @CRLF)
    $sDriverLatest = BinaryToString($sDriverLatest, $SB_UTF16LE)
    If Asc($sDriverLatest) = 63 Then $sDriverLatest = StringMid($sDriverLatest,2) ; skip ?
EndIf
ConsoleWrite("Latest=" & $sDriverLatest & @CRLF)

Is there a more reliable way to eliminate these unicode characters?

Dan

Share this post


Link to post
Share on other sites

This is more or less what I do in AutoIt3Wrapper to detect the different BOM options and later strip the inputfile, and later add it back.

Local $hTest_UTF = FileOpen("filename", 16)
Local $Test_UTF = FileRead($hTest_UTF, 4)
Local $i_Rec_Param, $i_Rec_Value, $Temp_Val
FileClose($hTest_UTF)
;~ 00 00 FE FF UTF-32, big-endian
;~ FF FE 00 00 UTF-32, little-endian
;~ FE FF UTF-16, big-endian
;~ FF FE UTF-16, little-endian
;~ EF BB BF UTF-8
Select
    Case BinaryMid($Test_UTF, 1, 4) = '0x0000FEFF'                   ; UTF-32 BE
        $UTFtype = '32BE'
    Case BinaryMid($Test_UTF, 1, 4) = '0xFFFE0000'                   ; UTF-32 LE
        $UTFtype = '32LE'
    Case BinaryMid($Test_UTF, 1, 2) = '0xFEFF'                       ; UTF-16 BE
        $UTFtype = '16BE'
    Case BinaryMid($Test_UTF, 1, 2) = '0xFFFE'                       ; UTF-16 LE
        $UTFtype = '16LE'
    Case BinaryMid($Test_UTF, 1, 3) = '0xEFBBBF'                     ; UTF-8
        $UTFtype = '8'
    Case Else
        $UTFtype = ''
EndSelect

Jos


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Thanks @Jos. This is what I ended up with (subset of _WD_UpdateDriver) --

Case 'msedge'
                $sVersionShort = StringLeft($sBrowserVersion, StringInStr($sBrowserVersion, ".") - 1)
                $sDriverLatest = __WD_Get('https://msedgedriver.azureedge.net/LATEST_RELEASE_' & $sVersionShort, 2) ; 2 = binary data

                If @error = $_WD_ERROR_Success Then
                    Select
                        Case BinaryMid($sDriverLatest, 1, 4) = '0x0000FEFF'                   ; UTF-32 BE
                            $iStartPos = 5
                            $iConversion = $SB_UTF16LE
                        Case BinaryMid($sDriverLatest, 1, 4) = '0xFFFE0000'                   ; UTF-32 LE
                            $iStartPos = 5
                            $iConversion = $SB_UTF16LE
                        Case BinaryMid($sDriverLatest, 1, 2) = '0xFEFF'                       ; UTF-16 BE
                            $iStartPos = 3
                            $iConversion = $SB_UTF16BE
                        Case BinaryMid($sDriverLatest, 1, 2) = '0xFFFE'                       ; UTF-16 LE
                            $iStartPos = 3
                            $iConversion = $SB_UTF16LE
                        Case BinaryMid($sDriverLatest, 1, 3) = '0xEFBBBF'                     ; UTF-8
                            $iStartPos = 4
                            $iConversion = $SB_UTF8
                        Case Else
                            $iStartPos = 1
                            $iConversion = $SB_ANSI
                    EndSelect

                    $sDriverLatest = StringStripWS(BinaryToString(BinaryMid($sDriverLatest, $iStartPos), $iConversion), $STR_STRIPTRAILING)
                    $sURLNewDriver = "https://msedgedriver.azureedge.net/" & $sDriverLatest & "/edgedriver_"
                    $sURLNewDriver &= ($lFlag64) ? "win64.zip" : "win32.zip"
                Else
                    $iErr = $_WD_ERROR_GeneralError
                EndIf
        EndSwitch

It appears to work for the current file, but honestly not sure that I've got the $iConversion stuff correct.

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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By GillesMaisonneuve
      Good morning,
       
      I am trying to read a Unicode utf8 string from a perl subprocess via StdoutRead.
      I use an AUtoIt GUI and display result in an 'Edit' control (see my code below) using 'Courier New', a font that can handle Unicode characters.
      I was expecting a result looking like (CMD console):
      ++$ chcp 65001>NUL: & perl -Mutf8 -CS -e "use 5.018; binmode STDOUT,q(:utf8); say qq(\x{03A9})" & chcp 850>NUL: Ω Instead I get someting like this (see downward the screen copy):
      ++$ chcp 1250>NUL: & perl -Mutf8 -CS -e "use 5.018; binmode STDOUT,q(:utf8); say qq(\x{03A9})" & chcp 850>NUL: Ω Obviously while I was expecting to receive an utf8 char, it seems to have been converted to Windows ANSI codepage 1250 (Windows default for Western/Central Europe, right ?)
      What am I doing wrong? Is there someone who could guide me?
       
      Here is my code and my output in the GUI.
      Creating and configuring the Edit control:
      Local $Edit1 = GUICtrlCreateEdit( "", 20, 110, 780, 500, BitOr($GUI_SS_DEFAULT_EDIT,$ES_MULTILINE,$ES_READONLY) ) GUICtrlSetData($Edit1, "This field will contain text result from external Perl command") GUICtrlSetFont($Edit1, 10, $FW_THIN, $GUI_FONTNORMAL, "Courier New")  
      Executing Perl command (note: `-Mutf8` and `-CS` garantees that I work in utf8 and STDOUT accepts wide-characters):
      local $ExePath = 'perl.exe -Mutf8 -CS ' ;~ if perl in PATH, no need for full path C:\Perl\bin\perl.exe local $Params = '-e "use 5.018; use utf8; use charnames q(:full); binmode STDOUT,q(:utf8);' & _ 'say scalar localtime; say qq(\N{GREEK CAPITAL LETTER OMEGA})"' local $Cmd = $ExePath & ' ' & $Params Local $iPID = Run($Cmd, "", @SW_HIDE, BitOR($STDERR_CHILD, $STDOUT_CHILD))  
      Reading STDOUT and displaying it into the Edit control:
      While 1 $sOutput &= StdoutRead($iPID) If @error Then ; Exit the loop if the process closes or StdoutRead returns an error. ExitLoop EndIf WEnd If $sOutput <> '' Then GUICtrlSetData($Edit1, $sOutput) EndIf  
      And now, what I get on my GUI:
       

    • By Skysnake
      'F‌inal' ; <========= problem between F and i 'F‌inal' ; <========= problem between F and i 'Final' ; <========= Notepad ANSI, no problem
      F‌inal

      PostgreSQL complains about that red dot.  Notepad++ marks the first word.
      Notepad said it contains Unicode.  Saved as ANSI and put back. Red dot is gone.
       
      the Text is inserted manually into an AutoIt Input box.  Things get done to it, and it ends up in a SQL database.  The data seems fine, but when I start generating reports, all kinds of funny problems show up.
       
      If I can ID that character, I can remove it.
      Any ideas?
    • By nacerbaaziz
      Hi dear
      With this script you can print Unicode text in the CMD screen
      the script is  easy to use
      just you  write the text that contains Unicode in the first input
      and the script automatically reflect the code in the second input
       you can copy the text to the clipboard
      or you can try printing the text in the CMD window
      I apologize to everyone for colors and shape if not appropriate
      I'm a blind man and I do not see
      Thank you for your understanding
      Greetings to all of youCmdUtM.au3
    • By rootx
      I need help with unicode char ü I get some text from online json but if try to read 4 example Zürich I heave  Zürich.
      How can I convert with autoit unicode to a clear character readable? thx
    • By legend
      Hello
      I'm using the code below to send mails using our internal relay server. 
      We got a Helpdesk system named Remedy. Our users can send us a mails using outlook 2010, and we'll get a ticket.
      The problem is if I send a mail using the above script our ticket system can't display unicode characters, like ex: Æ Ø Å. It will display them as: questionmarks: "? ? ?" inside our ticket system. In the outlook inbox it looks fine showing unicode symbols, but in our ticket system the unicode characters will be replaced by questionmarks.
      The thing is, if they send a mail using outlook, it works fine, but using the script above it doesen't.
      I tried to save my script with encoding: UTF-8 with BOM, but it didn't fix it.
       
      All suggestions are very welcome
       
       
       
       
×
×
  • Create New...