AutoHotkey Community

It is currently May 27th, 2012, 5:28 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: March 11th, 2010, 5:38 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
Ahk Compatible: Vanilla / Mainstream
License: New BSD License

Download (~11 kb)
    The archive contains the library, a demo script and the documentation. Also all required modules are included.

Online Documentation

Description

I tried to make a function to read textual portion of binary files. After searching the forum, I did not find any solution (that day). The function can be used to read out headers etc.

Examples

Code:
MsgBox % str := binGetString(bindata, 800, 64, filename)


Code:
#Include binGetString.ahk
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%
SetBatchLines, -1

; Any positiv number is the size of first bytes to load.
sizeToRead := 5000

; Specify how many characters to leave before
; beginning to work with file.
seek := 0

; Select source file
FileSelectFile, filepath, %A_WorkingDir%,, Select binary file
If (ErrorLevel = 0)
{
   ; Read file into memory
   FileRead, bindata, %filepath%
   
   ; Strip non printable ASCII characters from file
   str := binGetString(bindata, sizeToRead, seek, filepath)
   
   ; Show that content now
   MsgBox,, %filepath%, %str%
}


Edit: Name changed from binfile to binGetString. In general, it is the same function. In addition, it does leave out the first 31 ascii characters too.

_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--


Last edited by Tuncay on September 29th, 2010, 10:30 am, edited 5 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 11th, 2010, 6:23 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
The expression
Code:
Chr(NumGet(_data, _offset + A_Index - 1, "UChar"))
gives you the char already in _data, so you just copy it to str, except NUL characters are dropped. The ByRef _data already contains the result, in full. If you know there is no NUL in the _data, SubStr is easier to extract the desired bytes.


Last edited by Laszlo on March 11th, 2010, 6:36 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 11th, 2010, 6:34 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
I am not understanding why? The data contains nulls like in say mp3 or wma file. There IS nul in the data and this function converts to string representation. It is not desired for reading text files. :shock:

_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 11th, 2010, 6:39 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
When you append NUL {=chr(0)} to a string, nothing changes. This is why NUL characters are removed from str, which you return. Its length will be smaller, and you will not know, where the original NULs were.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 11th, 2010, 6:49 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
This is why I made this. Its more for like reading header of mp3 or such a thing.

_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 11th, 2010, 7:01 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
I see. The script description does not tell that you wanted to extract ANSI text from binary files, or drop NUL chars. If you want only ANSI text, you could exclude other funny characters, too, (with code <32 or >126) making the result even more readable.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 11th, 2010, 7:23 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
Ok I have renamed the function from binary_read() to binfile() and changed topic.
You are right about stripping characters above 126, but with those of below 32, there are some characters like tab, which should not be removed. I have to look which codes can be eliminated. But I think this should not be really neccessary, because a correct usage of this function assumes the user knows the offsets and size of data to extract.

Edit: Stripping all codes above 126, is this correct?

_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 11th, 2010, 8:51 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
If you are expecting diacritical letters, or other special characters you need to filter several intervals, or use "if var is alpha" for letters, and intervals for symbols. The script is useful for finding error message texts in program files, to determine the version of a document stored in binary form, to figure what SW created a file, etc. In these cases you might not know the offset, where to look.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 29th, 2010, 10:31 am 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
Updated and license changed.

_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Google Feedfetcher, sks, Stigg and 12 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group