Jump to content
BinaryBrother

ConsoleWrite is converting binary to string

Recommended Posts

BinaryBrother

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
UEZ

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
jchd

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
BrewManNH

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
BinaryBrother

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
jchd

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
BinaryBrother

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
  • Like 1

Share this post


Link to post
Share on other sites
jchd

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

    • nooneclose
      By nooneclose
      I need help turning this string "20180913221626" into a formatted time string.
      I need to go from this: 20180913221626
      to this: 09/13/2018 10:16 PM
      I do not always know what the date will be so I can not just use a variable I need to actually convert/format. 
      I did see an older post in the forms that was basically the same question only the other guy did not post the working code and I can not figure out how to use _AD_GetObjectProperties properly to get what I want. 
      As always any help would be appreciated. 
      Here is the code I use to find the date, but it always gives me the unformatted version. 
      ;retrieve the items object $oItem = $oOutlook.Session.GetItemFromID($aItems[1][0], Default) $oItem.GetInspector $eSentOn = $oItem.SentOn ; When was the email sent? MsgBox("", "Sent On of the email", $eSentOn) ;******************************************************************************* ; Formats the date and time from the email ;******************************************************************************* ;Local $fDatenTime = _DateTimeFormat($eSentOn, 1) ;MsgBox("", "Formatted email time", $fDatenTime) $aProperties = _AD_GetObjectProperties($eSentOn) _ArrayDisplay($aProperties, "Did the conversion work?")  
    • Danyfirex
      By Danyfirex
      Hello guys.  It's been awhile since I shared something. So today I want to share my last project.
      What's Loga?
      Loga is a simple logging library to keep track of code with an integrated console.
      Features.
      Common log levels. Integrated console. Multiple instances. Custom color and font for each instance log level. Define output format with macros. Conditional and occasional Logging. Easy to use. Basic Usage:
      #include "..\Loga.au3" ;This are some of the default settings: ;Default log level $LOGA_LEVEL_TRACE ;output format: {Symbol}{LogIndex} {LevelName} {LongDateTime} {Message} ;Log to File is enabled. ;Log file name format: YYYYMMDDHHMM-Loga-InstanceIndex.log ;Custom Console is disabled by default. ;By default log to STDOUT. _LogaTrace("I'm Trace") _LogaDebug("I'm Debug") _LogaInfo("I'm Info") _LogaWarn("I'm Warn") _LogaError("I'm Error") _LogaFatal("I'm Fatal")  
      More examples here.
       
      Check Loga on GitHub.
       
      Loga Latest Release v1.0.1.
       
      Saludos
       
       
       
       
    • Miliardsto
      By Miliardsto
      I got that func
      Func makeHelpImgGUI($title,$width,$height,$img) $img = GUICtrlCreatePic("",20,40,$width,$height) _ResourceSetImageToCtrl($img, "HERE") EndFunc and I call this func like that
      makeHelpImgGUI("Image",1190, 800,$SETTINGS_JPG)  
      so what is the problem in the parameter where is - "HERE" I need value of img but passed as string
      so $img = $SETTINGS_JPG and how make it "SETTINGS_JPG"
       
      I tried something like that but not work
      Func makeHelpImgGUI($title,$width,$height,$img) $name_str = String($img) $name_str = StringTrimLeft ($name_str, 1 ) $img = GUICtrlCreatePic("",20,40,$width,$height) _ResourceSetImageToCtrl($img, $name_str) EndFunc  
    • liagason
      By liagason
      Hello everyone,
      How can I display in ascending  sequence some numbers stored in a string variable?
      $str = "18,03,48,23" MsgBox(0,"test",$str) I would like it to display "03,18,23,48"
    • Rskm
      By Rskm
      Hi, I have the following line in a text file 'input.txt'. I know the line number - say '6'. I wish to replace the text 'WWW' in the below line with a random number (I can generate that with random()).
      WERIS  WWWJP   3.83  8.330  1.000                1097.RAXX 
      The WWW is a 3 digit integer (could be any number between 0 to 999), I can use stringtrimleft and get the numerical value of WWW in this file
      so, basically, I know the string to replace (ie; WWW stored in a variable), I know the line number to work on and the file location/name and the replacement variable (through random()). My requirement is to fill that 3 spaces with my random number (which Is a integer between 1 and 999)
      please put ur suggestions
       
×