#3764 closed Feature Request (Fixed)
ConsoleWrite binary mode
| Reported by: | Owned by: | Jon | |
|---|---|---|---|
| Milestone: | 3.3.16.1 | Component: | AutoIt |
| Version: | Severity: | None | |
| Keywords: | ConsoleWrite | Cc: |
Description
ConsoleRead has a binary mode, so it seems fitting that ConsoleWrite should have a binary mode as well. for backwards-compatibility it should be off-by-default and optional, i suggest changing it to
ConsoleWrite ( "data" [, binary = False] )
here is test code to see how ConsoleWrite currently handles binary data:
$test=""; for $i = 0 To 255 $test = $test & Chr($i); Next ConsoleWrite($test);
- it prints absolutely nothing, because ConsoleWrite stops writing at the first null-byte, which is generated by the first Chr($i=0) call. related forum thread at https://www.autoitscript.com/forum/topic/202893-are-string-variables-binary-safe/
Attachments (0)
Change History (5)
comment:1 by , 6 years ago
| Resolution: | → Rejected |
|---|---|
| Status: | new → closed |
comment:2 by , 6 years ago
BinaryToString is not a good solution, it would corrupt binary data, also for some reason, this prints absolutely nothing:
ConsoleWrite(BinaryToString(Chr(0) & "ABCDEFG"));
- absolutely nothing will be printed, but even if it was printed, a conversion to something non-binary would cause data corruption
lets take a more complex, and real, example: trying to implement the Unix Cat program in AutoIt ( https://en.wikipedia.org/wiki/Cat_(Unix) ),
a program available on all Linux and MacOS systems, but not shipped with Windows,
a simple implementation would probably look something like:
; PS! this code must be compiled with Aut2exe.exe with the /console argument!
; first read stdin
Local $stdinData
While True
$stdinData = ConsoleRead(False,True);
If @error Then ExitLoop
If(StringLen($stdinData) == 0) Then ExitLoop
ConsoleWrite($stdinData);
WEnd
; then read all files in arguments
For $i = 1 To $cmdLine[0] Step 1
ConsoleWrite(FileRead($cmdLine[$i]));
Next
but this script would fail to cat any files with NULL bytes in them,
and if you did ConsoleWrite(BinaryToString(FileRead($cmdLine[$i]))); , then it would *corrupt* binary files, wouldn't it?
comment:3 by , 5 years ago
@Jpm if you really believe that "stdout data corruption is an acceptable alternative to a binary write mode", then you're a moron.
comment:4 by , 4 years ago
| Milestone: | → 3.3.15.6 |
|---|---|
| Owner: | set to |
| Resolution: | Rejected → Fixed |
Fixed by revision [12718] in version: 3.3.15.6
comment:5 by , 4 years ago
| Milestone: | 3.3.15.6 → 3.3.16.1 |
|---|
Fixed by revision [12721] in version: 3.3.16.1

If you know your parameter to ConsolWrite is a binary just use ConsolWrite(BinaryToString($Data))
we will not Change the ConsoleWrite for such easy handling of Binary if needed
Cheers