Jump to content

ConsoleWrite is converting binary to string


Recommended Posts

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

SIGNATURE_0X800007D NOT FOUND

Link to comment
Share on other sites

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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

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

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

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

SIGNATURE_0X800007D NOT FOUND

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

Link to comment
Share on other sites

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

SIGNATURE_0X800007D NOT FOUND

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

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

×
×
  • Create New...