Jump to content
Sign in to follow this  
JohnW

Convert file to binary and then binary to file

Recommended Posts

JohnW

I am trying to take a simple txt file and convert it from the file to binary and then take that binary and convert it back to the original file.

Here is how I thought it should work but I was wrong.

ReadBlob("c:\bcmsdb.sql")

Func ReadBlob($Source)
    
    $SourceFile = FileOpen($Source,16)

    While 1 
        $FileData = FileRead($SourceFile)
        If @error = -1 Then ExitLoop

    Wend
    
    $FileData = Binary($FileData)

    $OutFile = FileOpen("C:\bcmsdb2.sql",18)
    FileWrite($OutFile,$FileData)
EndFunc

Share this post


Link to post
Share on other sites
Mobius

Converting a text file to binary will have no real effect. The output will be the same.

So are you trying to convert a file to Hex?

Vlad

Ed: oh, I see what you are asking now.... man your script is unwell.

Edited by Mobius

Share this post


Link to post
Share on other sites
SmOke_N

$FileData = FileRead($SourceFile)

should that be : ?

$FileData &= FileRead($SourceFile)

Why would it be in a loop anyway?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
goldenix

This is what I use:

;~ _convert file to binary_[ORIGINAL by Fabry].au3

$file = FileOpenDialog('Open the image file', @ScriptDir, 'image (*.mp3;*.jpg;*.bmp;*gif;*.png;*.txt;*.au3;*.exe)', 1)

$file2 = FileSaveDialog('Save the file', @ScriptDir, 'script (*.au3)')

_CheckExt($file2, 'au3')

$var = InputBox('Name variable', 'Prompt the name variable used', '$pic')

_CheckCapital($var, '$')

$hfile = FileOpen($file, 16)
$hfile2 = FileOpen($file2, 2)

FileWrite($hfile2, $var&'="0x"' & @CRLF)

While 1
    $data = FileRead($hfile, 4); how many chars we read at the time $data = FileRead($hfile)will read whole file
    If @error Then ExitLoop
    FileWrite($hfile2, $var&'&="' & StringTrimLeft($data,2) & '"' & @CRLF)
WEnd

MsgBox(0, 'Done!!', 'Done by Fabry!!!')

Func _CheckExt(ByRef $s_file, $s_ext)
    If StringRight($s_file, StringLen($s_ext)) <> $s_ext Then $s_file &= '.' & $s_ext
EndFunc   ;==>_CheckExt

Func _CheckCapital(ByRef $s_file, $s_cap)
    If StringLeft($s_file, StringLen($s_cap)) <> $s_cap Then $s_file = $s_cap & $s_file
EndFunc   ;==>_CheckExtoÝ÷ Ù«­¢+Ø쥹ÉäQ¼¥±½¹ÙÉÑÈ((¥¹±Õ±Ðíe½ÕÈ    ¥¹É䥱¹ÔÌÐì((ÀÌØí¥±ô¥±=Á¹¥±½ Ìäí=Á¸Ñ¡   ¥¹É䥱Ìäì°MÉ¥ÁѥȰÌäí   ¥¹Éä ¨¹Ô̤Ìäì°Ä¤((ÀÌØí¥±Èô¥±MÙ¥±½ ÌäíMÙÑ¡¥±Ìäì°MÉ¥ÁѥȰÌäì ¨¸¨¤Ìäì¤()1½°ÀÌØíÙÈôÅÕ½ÐìÁàÅÕ½Ðì()¥±]É¥Ñ ÀÌØí¥±È°   ¥¹Éä ÀÌØíÙȤ¤

My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]

Share this post


Link to post
Share on other sites
JohnW

I will try the suggestions. This is the first step in the process of taking a file converting it to binary and then saving that binary data into an Access database. Then I would take that data and convert it back to the original file.

I am hoping for something similar to

KB210486

I figured the first step was to see if I could convert a text file to binary and then write it back to a file. Which is also why it was in a loop.

Mobius,

I am still new at AutoIt so the code probably is very badly written.

Edited by JohnW

Share this post


Link to post
Share on other sites
JohnW

It seems by removing the loop I can now take the data and write it to a new file. Next step is to move it to the database and then read it back.

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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.