BinaryBrother

ConsoleWrite is converting binary to string

9 posts in this topic

#1 ·  Posted (edited)

Here is my code. Adopted from a post UEZ made.

_GenerateUID()
Func _GenerateUID()
    Local $lReturn, $lDriveSerial,$lRandomSeed = ""
    Local $lChr[2]
    For $N = 1 To 32
        $lChr[0] = Chr(Random(65, 90, 1)) ;A-Z
        $lChr[1] = Chr(Random(48, 57, 1)) ;0-9
        $lRandomSeed &= $lChr[Random(0, 1, 1)]
    Next
    $lDriveSerial = DriveGetSerial(StringLeft(@WindowsDir, 3))
    $lReturn = StringToBinary($lRandomSeed & $lDriveSerial)
    ConsoleWrite($lReturn) ;<--- Is printed as a String.
    ConsoleWrite(@CRLF & $lReturn & @CRLF) ;<--- Printed properly as Binary?
    Return $lReturn
EndFunc   ;==>_GenerateUID

Output:

--> Press Ctrl+Alt+Break to Restart or Ctrl+Break to Stop
V9P03Y144OZW38YC80IW1IJXWH4003501486415223
0x5639503033593134344F5A57333859433830495731494A58574834303033353031343836343135323233
+>10:40:52 AutoIt3.exe ended.rc:0
+>10:40:52 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 0.544


Whenever ConsoleWrite is fed pure binary, it will output it as a string. Whenever a string is mixed with the binary, it outputs the binary as binary.

I found this especially confusing when I consulted the Help Documents for an answer...

Binary data is written as-is. It will not be converted to a string. To print the hex representation of binary data, use the String() function to explicitly cast the data to a string.

 

I'm sure I'm missing something, as usual. As always, thanks for your time! ^_^

Edited by BinaryBrother

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Seems to be a bug. Once something different than the variable is executed the string becomes a binary string.

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

There is no bug here: MyFunc($BinaryVar) operates on the binary variable passed, but MyFunc("abc" & $BinaryVar) operates on the result of the expression, where the & operator forces conversion of $BinaryVar to a string, hence MyFunc is passed a string in this case.

ConsoleWrite is no different, except that it also converts AutoIt (Unicode) strings to ANSI and this causes non-Ansi characters to be replaced by question marks.


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

There's a track ticket about this issue #2688.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

There is no bug here: MyFunc($BinaryVar) operates on the binary variable passed, but MyFunc("abc" & $BinaryVar) operates on the result of the expression, where the & operator forces conversion of $BinaryVar to a string, hence MyFunc is passed a string in this case.

ConsoleWrite is no different, except that it also converts AutoIt (Unicode) strings to ANSI and this causes non-Ansi characters to be replaced by question marks.

The only thing is, it converts the binary to a string without using the '&' operator. The below is outputting a string to the console, despite being binary.

$Test = StringToBinary(DriveGetSerial("C:\"))
ConsoleWrite($Test)

Since ConsoleWrite is being fed binary and does not convert binary to string, should it not output binary?

Edited by BinaryBrother

Share this post


Link to post
Share on other sites

From the specification of ConsoleWrite in the help file, no!

At the end of the day what we experience here is perfectly logical and conform, relative to specifications. That it may not directly fit somebody (you, me, anyone) today's or tomorrow's need is a complete different matter. Anyway, it's very easy to make it output exactly what's wanted, one way or the other.


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

#7 ·  Posted (edited)

This example just confuses me, so I'm going to stop poking the binary bear...

$Test = Binary(0x00204060)
ConsoleWrite("String: ")
ConsoleWrite($Test)
ConsoleWrite(@CRLF)
ConsoleWrite("Binary: " & $Test & @CRLF)
ConsoleWrite("MORE OUTPUT" & @CRLF)
ConsoleWrite("MORE OUTPUT" & @CRLF)
ConsoleWrite("MORE OUTPUT" & @CRLF)
ConsoleWrite("MORE OUTPUT" & @CRLF)
ConsoleWrite("MORE OUTPUT" & @CRLF)
ConsoleWrite("MORE OUTPUT" & @CRLF)
ConsoleWrite("MORE OUTPUT" & @CRLF)
ConsoleWrite("MORE OUTPUT" & @CRLF)
ConsoleWrite("MORE OUTPUT" & @CRLF)
ConsoleWrite("MORE OUTPUT" & @CRLF)
ConsoleWrite("MORE OUTPUT" & @CRLF)
ConsoleWrite("MORE OUTPUT" & @CRLF)
ConsoleWrite("MORE OUTPUT" & @CRLF)
ConsoleWrite("MORE OUTPUT" & @CRLF)

Output:

--> Press Ctrl+Alt+Break to Restart or Ctrl+Break to Stop
String: `@ +>11:36:54 AutoIt3.exe ended.rc:0
+>11:36:54 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 0.2671

The output completely dies after ConsoleWrite($Test)... :blink:

Yeah, the lack of displaying pure binary is easy to fix and doesn't effect anything, really, because a MsgBox() confirms the binary is fine. I just found it lightly confusing. At first, I thought some variables were stubbornly not converting to binary and messed with it for a while trying to force binary conversion a few different ways, before I found that ConsoleWrite was messing with me... :huh2:



Whoa... I know I'm starting to get off topic, but I just found a bunch of neat console tricks that I never knew!

ConsoleWrite('! = Red' & @CRLF) ; ! = red text color
ConsoleWrite('> = Blue' & @CRLF) ; > = blue text color
ConsoleWrite('- = Orange' & @CRLF) ; - = orange text color
ConsoleWrite('+ = Green' & @CRLF) ; + = green text color
ConsoleWrite('(5) : = Red (jump to line 5 when double-clicked)' & @CRLF) ; '(5) :' red text color and double click jumps to (line number)
ConsoleWrite('Start with String or Integer then ' & @TAB & '6' & ' = Pink (jump to line 6 when double-clicked)' & @CRLF)
; pink text color, any sign and then @TAB & 'line number'

; Example 1
ConsoleWrite('(' & @ScriptLineNumber & ') : = Red (jump to line ' & @ScriptLineNumber & ' when double-clicked)' & @CRLF)

; Clear console output pane of SciTE after 5000 ms
Sleep(5000)
ControlSend("[CLASS:SciTEWindow]", "", "Scintilla2", "+{F5}")

 

Edited by BinaryBrother
1 person likes this

Share this post


Link to post
Share on other sites

The first ConsoleWrite in your first binary example has a leading binary 0, which the console interprets as an end of string. That's why all subsequent ConsoleWrite don't show up.


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

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

  • Similar Content

    • ur
      By ur
      I have date in the string format as "DD-MM-YYYY".
       
      I need to get yesterday's date from it.
      I tried converting this from _DateTimeFormat but not working.
      Is there any direct UDF available to get this.?
    • rkr
      By rkr
      Hi, i want to read a particular string from a text file using autoit. i wish to read it without explicitly opening the text file. the copied string should be then transferred to an excelbook (again, no need to explicitly open the excel book)... 

      with reference to my screenshot attached, my input to the  'script'' is going to be 0017-0008, and the script should copy the highlighted two lines from the input file to excel
      thanks

    • Dragonfighter
      By Dragonfighter
      I'm searching a way to do xor and shift and if possible also other operations. Thanks in advance for the replies.
    • rudi
      By rudi
      Hello.
      I'm too stupid to see my mistake:
      To investigate the internal "dictionary" of TIFF files I'd like to read in the files in binary mode and to check, if there are more than one pages "in" this TIFF.
      Notepad++, "View as Hex" is presenting the first bytes as "49 49 2a 20 08 20 20 20 12" for the TIF attached to this posting
      The "TIFF Header Format" is easy:
      Offset 00h, 2 Byte = Byte Order, "II"=intel, "MM"=motorola. (I = 0x49)
      --> II
      Offset 02h, 2 Byte = Version Nr.
      Offset 04h, 4 Byte = pointer to first IFD entry
      Description of TIFF header: https://www.awaresystems.be/imaging/tiff/faq.html#q3
       

      Howto read and analyse the binary content correctly? This is my messy, not operational code:
       
      $sampleTiff="H:\daten\tif\11\11\111111.TIF" $h=FileOpen($sampleTiff,16) $content=FileRead($h) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $content = ' & $content & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console FileClose($h) $type=VarGetType($content) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $type = ' & $type & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console $ToString=BinaryToString($content) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $ToString = ' & $ToString & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console ConsoleWrite(@CRLF & @CRLF) $content=StringTrimLeft($content,2) ; cut off the leading "0x" ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $content = ' & $content & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console for $i = 1 to 8 step 8 $next=StringMid($content,$i,2) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $next = ' & $next & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console $Chr=BinaryToString($next) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Chr = ' & $Chr & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console ConsoleWrite(@CRLF & "---" & @CRLF) Next Regards, Rudi.
      111111.TIF
    • ur
      By ur
      When I am trying to compile the autoit files with aut2exe.
      I am getting below error.
      There is no issue in code as the same code is getting compiled on different machine.
      I tried reinstalling the AUtoIT, but the issue replicates.

      Any suggestions?