Jump to content

Recommended Posts

Posted

Hello all, 

So ive been trying to write data to a file:

$bin = "Test"
$hFile = FileOpen("test.bin", 17)
FileWrite($hFile, Binary($bin))
FileClose($hFile)

So the above example does not write the data in binary format, it just seems to write it in plain text

$bin = "Test"
$hFile = FileOpen("test.bin", 17)
FileWrite($hFile, Binary(0))
FileWrite($hFile, Binary($bin))
FileClose($hFile)

However this one does write it in binary format, I was just wondering why this was, and whether the additional line can be avoided.

Thanks in advance

Posted

StringToBinary('Test',....

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

$hFile = FileOpen("test.bin",  $FO_OVERWRITE+ $FO_BINARY)

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)

where in the string "Test" are you expecting the "Binary" data to come from?

Or more simply what sort of output were you expecting?

A string with a leading or trailing null byte?

Ed: Or perhaps you were hoping for a null delimited interpretation of "Test".

Edited by Mobius

wtfpl-badge-1.png

Posted

@Mobius, so when I enter the additional line, the string 'Test' is shown as 0000 0000 5465 7374 in the bin file (where the 0000 0000 is the '0' in the additional line and the 5465 7374 is the 'Test' string) but without the additional line, the bin file just shows 'Test' in plain text.

So I was just wondering why the additional line is needed,and how the information in the bin file can result in just the 5465 7374 without the additional line. 

Does that make sense?? Apologies if I'm not being very clear

Posted (edited)

here is SSEE (some self explanatory example)

#include <FileConstants.au3>
#include <MsgBoxConstants.au3>


Global $sText = InputBox("Testing", "Enter your text Here", "", " M")
Global $dTextAsBinaryData = StringToBinary($sText)

MsgBox(0,'$dTextAsBinaryData',$dTextAsBinaryData)
Example($dTextAsBinaryData)

ShellExecute(@ScriptDir & '\test.txt')

Func Example($dBINARYDATA)
    Local $hFile = FileOpen('test.txt', $FO_OVERWRITE + $FO_BINARY)
    FileWrite($hFile, $dBINARYDATA)
    FileClose($hFile)
EndFunc   ;==>Example
Edited by mlipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

  • Solution
Posted

The additional line is only necessary if you wanted your file to start with 4 nulls.

I am assuming AutoIt did this because Binary(0) is writing a long (4 byte number) with the value of 0.

A file that does not contain null space and you write the hex 54657374 (Test) is going to be treated as a text file because without your line that added nulls that is what it is.

So I have to ask again what were you expecting/wanting when you chose to write a binary file, and more importantly how were you planning on reading and using this data in your program?

wtfpl-badge-1.png

Posted

  On 2/26/2015 at 9:42 PM, Noviceatthis said:

@Mobius, that would explain it, thank you! I havn't decided what I want to do with it yet, I just wanted to figure out what was going on

Fair enough dude, I didn't mean to be repetitious with my questioning. :)

wtfpl-badge-1.png

  • 4 years later...
Posted

Although this is a very old entry, I think we, sometimes, forget that everything is binary when we write a file (any file, yes)
The difference from text files and binary files, is just that, in text files, we just have binary combination that shows you a character for each byte.

Posted

Not exactly.  The visual result merely depends on which program you use to "read" the file back: text editor, image viewer, nibble editor, ...

  Reveal hidden contents

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)

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.
×
×
  • Create New...