Jump to content

Convert binary to file


Recommended Posts

I use this to convert a file in binary mode.

$File_Handle = FileOpen($File_Path, 0+16)

$File_Content = FileRead($File_Handle)

FileClose($File_Handle)

FileWrite("prova.txt", $File_Content)

How can I convert the new file (in this case, prova.txt) back to the original file?

Thanks for help

Link to comment
Share on other sites

  • Moderators

FSoft,

Just open the new file for binary writing: ;)

$File_Path = "fred4.au3"

$File_Handle = FileOpen($File_Path, 0 + 16)
$File_Content = FileRead($File_Handle)
FileClose($File_Handle)

ConsoleWrite($File_Content & @CRLF)

FileWrite("prova.txt", $File_Content)

$File_Handle = FileOpen("Original.txt", 2 + 16)
FileWrite($File_Handle, $File_Content)
FileClose($File_Handle)

But have you looked at what is in prova.txt? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

@Melba: Thank you for your reply, but you don't understand what I'm trying to do.

I have the following script:

$File_Path = "test.zip"
$File_Handle = FileOpen($File_Path, 0+16)
$File_Content = FileRead($File_Handle)
FileClose($File_Handle)
FileWrite("prova.txt", $File_Content)
Exit

The file "prova.txt" contains the binary version of the file "test.zip".

Now I want another script to open the file "prova.txt" and convert it back to "test.zip". It's possible to do this?

Thank you for help!

Edited by FSoft
Link to comment
Share on other sites

Hey!! FSoft

Nice Meeting U

Maybe This is the UDF that will Do Your Work

Link:

Thumbs Up if it Helped ;)

Regards

Phoenix XL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

Hi Phoenix. Thank you for the answer, however that script uses encbin and decbin to do the work. I don't want to rely on 3rd part components.

It's possible to do everything in pure AutoIt?

Edited by FSoft
Link to comment
Share on other sites

I don't really understand. You have a binary file, and write it as Hex-encoded string in a new file. Now you are looking for a way to convert this string back into a binary file?

; read binary file
$hFile = FileOpen($sBinaryFile, 16)
$bBinary = FileRead($hFile)
FileClose($hFile)

; write hex file
$hFile = FileOpen($sStringFile, 2)
FileWrite($hFile, String($sBinary))
FileClose($hFile)

; --------------------------------------

; read hex file
$hFile = FileOpen($sStringFile)
$sString = FileRead($hFile)
FileClose($hFile)

; write new binary file
$hFile = FileOpen($sNewBinaryFile, 18)
FileWrite($hFile, Binary($sString))
FileClose($hFile)

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

@ProgAndy: That doesn't work. I tried with a PDF and the new file created is corrupted.

There was a small typo. This works for me:
$sBinaryFile = "test.png"
$sStringFile = "testbin.txt"
$sNewBinaryFile = "test_out.png"

; read binary file
$hFile = FileOpen($sBinaryFile, 16)
$bBinary = FileRead($hFile)
FileClose($hFile)

; write hex file
$hFile = FileOpen($sStringFile, 2)
FileWrite($hFile, String($bBinary))
FileClose($hFile)

; --------------------------------------

; read hex file
$hFile = FileOpen($sStringFile)
$sString = FileRead($hFile)
FileClose($hFile)

; write new binary file
$hFile = FileOpen($sNewBinaryFile, 18)
FileWrite($hFile, Binary($sString))
FileClose($hFile)

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...