Jump to content

How to read/extract binary data?


Recommended Posts

Pardonb my ignorance, I had a hard time extracting binary info from a binary datafile.

I am struggling reading 8-bytes of data, reading them in hexadecimal right to left.

Below is part of my working script, but i believe there is a much efficient approach using Binary and BinaryMid functions.

$file = FileOpen($binfile, 16)

If $file = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

$size = FileGetSize($binfile)

$newdatasize = $size - 691280

$junkdata = FileRead($file, 691280)

$usefuldata = FileRead($file, $newdatasize)

$hex1 = StringMid($usefuldata, 3, 2)

$hex2 = StringMid($usefuldata, 5, 2)

$hex3 = StringMid($usefuldata, 7, 2)

$hex4 = StringMid($usefuldata, 9, 2)

$hexdata = "0x" & $hex4 & $hex3 & $hex2 & $hex1

$epochsec = Number ($hexdata)

MsgBox(0,"hexdata",$hexdata & "-"& $epochsec,10)

$creationdate = _DateAdd("s", $epochsec, "1970/01/01 00:00:00")

Anyone would direct me to a better design?

Apart from the above issue, i had yet to find a good solution to extract the data.

The situation is: I got few data files, I had managed to spool the useful data without the junk data. Now my new datafile had a data structure of 34 bytes of info header followed by a JPEG stream, and continue to the next 34 bytes, then JPEG data stream until EOF. Of the 34 bytes, the first 4 bytes contain the epoch seconds (seconds since 1970/1/1 00:00:00), the JPEG stream start with FF D8 FF and ends with FF D9. I need to spool the JPEG data(s) with with filename linked to the individual epoch seconds date/time.

Any help for this?

Thanks in advance..

Regards,

Kevin Lai.

Link to comment
Share on other sites

Pardonb my ignorance, I had a hard time extracting binary info from a binary datafile.

I am struggling reading 8-bytes of data, reading them in hexadecimal right to left.

Below is part of my working script, but i believe there is a much efficient approach using Binary and BinaryMid functions.

$file = FileOpen($binfile, 16)

If $file = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

$size = FileGetSize($binfile)

$newdatasize = $size - 691280

$junkdata = FileRead($file, 691280)

$usefuldata = FileRead($file, $newdatasize)

$hex1 = StringMid($usefuldata, 3, 2)

$hex2 = StringMid($usefuldata, 5, 2)

$hex3 = StringMid($usefuldata, 7, 2)

$hex4 = StringMid($usefuldata, 9, 2)

$hexdata = "0x" & $hex4 & $hex3 & $hex2 & $hex1

$epochsec = Number ($hexdata)

MsgBox(0,"hexdata",$hexdata & "-"& $epochsec,10)

$creationdate = _DateAdd("s", $epochsec, "1970/01/01 00:00:00")

Anyone would direct me to a better design?

Apart from the above issue, i had yet to find a good solution to extract the data.

The situation is: I got few data files, I had managed to spool the useful data without the junk data. Now my new datafile had a data structure of 34 bytes of info header followed by a JPEG stream, and continue to the next 34 bytes, then JPEG data stream until EOF. Of the 34 bytes, the first 4 bytes contain the epoch seconds (seconds since 1970/1/1 00:00:00), the JPEG stream start with FF D8 FF and ends with FF D9. I need to spool the JPEG data(s) with with filename linked to the individual epoch seconds date/time.

Any help for this?

Thanks in advance..

Regards,

Kevin Lai.

Try BinaryMid instead of StringMid
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Try BinaryMid instead of StringMid

Yes, I tried that before.

$hex1 = BinaryMid ($usefuldata,1,1)

$hex2 = BinaryMid ($usefuldata,2,1)

$hex3 = BinaryMid ($usefuldata,3,1)

$hex4 = BinaryMid ($usefuldata,4,1)

The problem is, I had failed trying to convert them back to integer/number using Int() or Number() to get the value into var $epochsec.

But i believe each $hex* var of the above value had an additional prefix of "0x", it is more efficient to get them into string value to merge into a 4 bytes strings in right to left hex value orientation.

Edited by kevinlaikf
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...