Scripts for reading and writing binary files

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Scripts for reading and writing binary files

Alejandro Tejada
Hi all,

Recently, I had to test and modify some binary files,
so digging in my own stacks,  I found these scripts:

[WARNING: The script on step 3, OVERWRITE the
original file. If you do not want this, simply
change the path and/or name of the file]

1) create a field to hold the converted
binary to hexadecimal data.
Set the name of this field to: "tHex"

2) Create a button named "Import" to hold
the following script:
----------------------------------------------------
global tFile

on mouseUp
  answer file "select"
  if it is not empty then
    put "binfile:"&it into tFile
      put url tFile into tData
      -- convert binary data to Hexadecimal
      repeat for each char n in tData
        put chartonum(n) into nft
        put baseconvert(nft,10,16) into kjh
        if the number of chars of kjh < 2 then put "0" before kjh
        put kjh & space after cdf
      end repeat
      put cdf into fld "tHex"
end mouseUp
-----------------------------------------------------
Now, you have the field "tHex" with Hexadecimal numbers,
like these:

46 57 53 03 4F 00 00 00 78 00 05 5F 00 00 0F A0
00 00 0C 01 00 43 02 FF FF FF BF 00 23 00 00 00
01 00 70 FB 49 97 0D 0C 7D 50 00 01 14 00 00 00
00 01 25 C9 92 0D 21 ED 48 87 65 30 3B 6D E1 D8
B4 00 00 86 06 06 01 00 01 00 00 40 00 00 00

After you make all the modifications to
the file (using hexadecimal), save it
back as a binary file:

3) Create a button named "Export"
and paste this script:

------------------------------------------------------
global tFile

on mouseUp
put fld "tHex" into tData

repeat for each word n in tData
put numtochar(baseconvert(n,16,10)) after tBinary
end repeat

put tBinary into url tFile
end mouseUp
------------------------------------------------------

Thanks in advance for your
opinions and comments!

Al
Reply | Threaded
Open this post in threaded view
|

Re: Scripts for reading and writing binary files

Paul Dupuis
Of course, now if you use these scripts, you should replace 'char' with
'byte' for the eventual day the two are no longer synonymous.

On 12/16/2011 8:03 AM, Alejandro Tejada wrote:

> Hi all,
>
> Recently, I had to test and modify some binary files,
> so digging in my own stacks,  I found these scripts:
>
> [WARNING: The script on step 3, OVERWRITE the
> original file. If you do not want this, simply
> change the path and/or name of the file]
>
> 1) create a field to hold the converted
> binary to hexadecimal data.
> Set the name of this field to: "tHex"
>
> 2) Create a button named "Import" to hold
> the following script:
> ----------------------------------------------------
> global tFile
>
> on mouseUp
>   answer file "select"
>   if it is not empty then
>     put "binfile:"&it into tFile
>       put url tFile into tData
>       -- convert binary data to Hexadecimal
>       repeat for each char n in tData
>         put chartonum(n) into nft
>         put baseconvert(nft,10,16) into kjh
>         if the number of chars of kjh < 2 then put "0" before kjh
>         put kjh & space after cdf
>       end repeat
>       put cdf into fld "tHex"
> end mouseUp
> -----------------------------------------------------
> Now, you have the field "tHex" with Hexadecimal numbers,
> like these:
>
> 46 57 53 03 4F 00 00 00 78 00 05 5F 00 00 0F A0
> 00 00 0C 01 00 43 02 FF FF FF BF 00 23 00 00 00
> 01 00 70 FB 49 97 0D 0C 7D 50 00 01 14 00 00 00
> 00 01 25 C9 92 0D 21 ED 48 87 65 30 3B 6D E1 D8
> B4 00 00 86 06 06 01 00 01 00 00 40 00 00 00
>
> After you make all the modifications to
> the file (using hexadecimal), save it
> back as a binary file:
>
> 3) Create a button named "Export"
> and paste this script:
>
> ------------------------------------------------------
> global tFile
>
> on mouseUp
> put fld "tHex" into tData
>
> repeat for each word n in tData
> put numtochar(baseconvert(n,16,10)) after tBinary
> end repeat
>
> put tBinary into url tFile
> end mouseUp
> ------------------------------------------------------
>
> Thanks in advance for your
> opinions and comments!
>
> Al
>
> --
> View this message in context: http://runtime-revolution.278305.n4.nabble.com/Scripts-for-reading-and-writing-binary-files-tp4204548p4204548.html
> Sent from the Revolution - User mailing list archive at Nabble.com.
>
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>

--
Paul Dupuis
Cofounder
Researchware, Inc.
http://www.researchware.com/
http://www.twitter.com/researchware
http://www.facebook.com/researchware
http://www.linkedin.com/company/researchware-inc


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Scripts for reading and writing binary files

Alejandro Tejada
Hi Paul,

Paul Dupuis wrote
Of course, now if you use these scripts, you should replace 'char' with
'byte' for the eventual day the two are no longer synonymous.
That would be after RunRev change all text functions,
commands and properties to work only in Unicode.

Thanks for adding this important caveat.

Al
Reply | Threaded
Open this post in threaded view
|

Re: Scripts for reading and writing binary files

Michael Kann
In reply to this post by Alejandro Tejada
Extremely useful. Thanks.

Michael



--- On Fri, 12/16/11, Alejandro Tejada <[hidden email]> wrote:

From: Alejandro Tejada <[hidden email]>
Subject: Scripts for reading and writing binary files
To: [hidden email]
Date: Friday, December 16, 2011, 7:03 AM

Hi all,

Recently, I had to test and modify some binary files,
so digging in my own stacks,  I found these scripts:

[WARNING: The script on step 3, OVERWRITE the
original file. If you do not want this, simply
change the path and/or name of the file]

1) create a field to hold the converted
binary to hexadecimal data.
Set the name of this field to: "tHex"

2) Create a button named "Import" to hold
the following script:
----------------------------------------------------
global tFile

on mouseUp
  answer file "select"
  if it is not empty then
    put "binfile:"&it into tFile
      put url tFile into tData
      -- convert binary data to Hexadecimal
      repeat for each char n in tData
        put chartonum(n) into nft
        put baseconvert(nft,10,16) into kjh
        if the number of chars of kjh < 2 then put "0" before kjh
        put kjh & space after cdf
      end repeat
      put cdf into fld "tHex"
end mouseUp
-----------------------------------------------------
Now, you have the field "tHex" with Hexadecimal numbers,
like these:

46 57 53 03 4F 00 00 00 78 00 05 5F 00 00 0F A0
00 00 0C 01 00 43 02 FF FF FF BF 00 23 00 00 00
01 00 70 FB 49 97 0D 0C 7D 50 00 01 14 00 00 00
00 01 25 C9 92 0D 21 ED 48 87 65 30 3B 6D E1 D8
B4 00 00 86 06 06 01 00 01 00 00 40 00 00 00

After you make all the modifications to
the file (using hexadecimal), save it
back as a binary file:

3) Create a button named "Export"
and paste this script:

------------------------------------------------------
global tFile

on mouseUp
put fld "tHex" into tData

repeat for each word n in tData
put numtochar(baseconvert(n,16,10)) after tBinary
end repeat

put tBinary into url tFile
end mouseUp
------------------------------------------------------

Thanks in advance for your
opinions and comments!

Al

--
View this message in context: http://runtime-revolution.278305.n4.nabble.com/Scripts-for-reading-and-writing-binary-files-tp4204548p4204548.html
Sent from the Revolution - User mailing list archive at Nabble.com.

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Scripts for reading and writing binary files

Alejandro Tejada
Hi Michael,

Michael Kann wrote
Extremely useful. Thanks.
You are welcome!

I received the request to include a function for
alerting about the file size, before opening it.

Additional ideas to make this more useful?

Al
Reply | Threaded
Open this post in threaded view
|

Re: Scripts for reading and writing binary files

Michael Kann
Alejandro,

It might be helpful to extract any ASCII text which is embedded in the binary. Then you can see what kind of file you're working with.

Example:

Standard MIDI music files have the ASCII code for "MThd" (4D 54 68 64)

Overview here:

http://en.wikipedia.org/wiki/Magic_number_%28programming%29

Thanks again,

Mike





--- On Fri, 12/16/11, Alejandro Tejada <[hidden email]> wrote:

From: Alejandro Tejada <[hidden email]>
Subject: Re: Scripts for reading and writing binary files
To: [hidden email]
Date: Friday, December 16, 2011, 9:26 AM

Hi Michael,


Michael Kann wrote
>
> Extremely useful. Thanks.
>

You are welcome!

I received the request to include a function for
alerting about the file size, before opening it.

Additional ideas to make this more useful?

Al


--
View this message in context: http://runtime-revolution.278305.n4.nabble.com/Scripts-for-reading-and-writing-binary-files-tp4204548p4205026.html
Sent from the Revolution - User mailing list archive at Nabble.com.

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Scripts for reading and writing binary files

Alejandro Tejada
Hi Michael,

Michael Kann wrote
It might be helpful to extract any ASCII text which is embedded in the binary. Then you can see what kind of file you're working with.
Example:
Standard MIDI music files have the ASCII code for "MThd" (4D 54 68 64)
Overview here:
http://en.wikipedia.org/wiki/Magic_number_%28programming%29
Check this stack:

http://andregarzia.on-rev.com/alejandro/stacks/Analize_Binary_file02.zip

Have a nice weekend!

Al
Reply | Threaded
Open this post in threaded view
|

Re: Scripts for reading and writing binary files

Michael Kann
Alejandro,

As always, you come through in amazing ways.

Mike

--- On Sun, 12/18/11, Alejandro Tejada <[hidden email]> wrote:

From: Alejandro Tejada <[hidden email]>
Subject: Re: Scripts for reading and writing binary files
To: [hidden email]
Date: Sunday, December 18, 2011, 10:00 AM

Hi Michael,


Michael Kann wrote
>
> It might be helpful to extract any ASCII text which is embedded in the
> binary. Then you can see what kind of file you're working with.
> Example:
> Standard MIDI music files have the ASCII code for "MThd" (4D 54 68 64)
> Overview here:
> http://en.wikipedia.org/wiki/Magic_number_%28programming%29
>

Check this stack:

http://andregarzia.on-rev.com/alejandro/stacks/Analize_Binary_file02.zip

Have a nice weekend!

Al

--
View this message in context: http://runtime-revolution.278305.n4.nabble.com/Scripts-for-reading-and-writing-binary-files-tp4204548p4211195.html
Sent from the Revolution - User mailing list archive at Nabble.com.

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Scripts for reading and writing binary files

Alejandro Tejada
Hi All,

I uploaded a new version of the stack "Analize Binary Files":

http://andregarzia.on-rev.com/alejandro/stacks/Analize%20_Binary_files03.livecode
http://at.livecodejournal.com/stacks/Analize%20_Binary_files03.livecode

Many Thanks to Andre Garzia and Richard Gaskin
for allowing me to store these files in their servers.

Have a nice day!

Al