emendelson Posted October 11, 2010 Posted October 11, 2010 (edited) I would be very grateful for any information on a fast and efficient way to put the contents of a binary file into an array. This question derives from this message (message 12 in the linked thread): http://www.autoitscript.com/forum/index.php?showtopic=120667&view=findpost&p=838917 In that message, Martin provided a way to put a binary string into an array, but he observes that his method is very slow, and suggested that I ask for a faster one. Can anyone offer a faster method? Many thanks for any help. Edited October 11, 2010 by Edward Mendelson
Ascend4nt Posted October 11, 2010 Posted October 11, 2010 (edited) $hFile=FileOpen("binary.file",16) $binBuf=FileRead($hFile) $iErr=@error $iBytes=@extended FileClose($hFile) If Not $iErr Then $aArray=StringRegExp(StringTrimLeft($binBuf,2),'..',3) ConsoleWrite("Bytes read:"&$iBytes&", Array size:"&UBound($aArray)&@CRLF) ;~ _ArrayDisplay($aArray,"Binary dump") ; (will take a lot of memory/time for big arrays) EndIf *edit: @error check instead of $iErr Edited October 11, 2010 by Ascend4nt My contributions: Performance Counters in Windows - Measure CPU, Disk, Network etc Performance | Network Interface Info, Statistics, and Traffic | CPU Multi-Processor Usage w/o Performance Counters | Disk and Device Read/Write Statistics | Atom Table Functions | Process, Thread, & DLL Functions UDFs | Process CPU Usage Trackers | PE File Overlay Extraction | A3X Script Extract | File + Process Imports/Exports Information | Windows Desktop Dimmer Shade | Spotlight + Focus GUI - Highlight and Dim for Eyestrain Relief | CrossHairs (FullScreen) | Rubber-Band Boxes using GUI's (_GUIBox) | GUI Fun! | IE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) | Magnifier (Vista+) Functions UDF | _DLLStructDisplay (Debug!) | _EnumChildWindows (controls etc) | _FileFindEx | _ClipGetHTML | _ClipPutHTML + ClipPutHyperlink | _FileGetShortcutEx | _FilePropertiesDialog | I/O Port Functions | File(s) Drag & Drop | _RunWithReducedPrivileges | _ShellExecuteWithReducedPrivileges | _WinAPI_GetSystemInfo | dotNETGetVersions | Drive(s) Power Status | _WinGetDesktopHandle | _StringParseParameters | Screensaver, Sleep, Desktop Lock Disable | Full-Screen Crash Recovery Wrappers/Modifications of others' contributions: _DOSWildcardsToPCRegEx (original code: RobSaunder's) | WinGetAltTabWinList (original: Authenticity) UDF's added support/programming to: _ExplorerWinGetSelectedItems | MIDIEx UDF (original code: eynstyne) (All personal code/wrappers centrally located at Ascend4nt's AutoIT Code)
martin Posted October 11, 2010 Posted October 11, 2010 $hFile=FileOpen("binary.file",16) $binBuf=FileRead($hFile) $iErr=@error $iBytes=@extended FileClose($hFile) If Not $iErr Then $aArray=StringRegExp(StringTrimLeft($binBuf,2),'..',3) ConsoleWrite("Bytes read:"&$iBytes&", Array size:"&UBound($aArray)&@CRLF) ;~ _ArrayDisplay($aArray,"Binary dump") ; (will take a lot of memory/time for big arrays) EndIf *edit: @error check instead of $iErr Actually it's "how to put the binary string into a dllstruct which is a byte array?". I thought that I could have a char array struct created at the same address as the byte array struct and do something like dllstructsetdata($Struct,1,BinaryToString($binBuf)) but it doesn't work. It is probably obvious but I've got a bit rusty with AutoIt. 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.
Ascend4nt Posted October 11, 2010 Posted October 11, 2010 (edited) Even simpler: $hFile=FileOpen("binary.file",16) $binBuf=FileRead($hFile) $iErr=@error $iBytes=@extended FileClose($hFile) If Not $iErr Then $stStr=DllStructCreate("byte["&$iBytes&"]") DllStructSetData($stStr,1,$binBuf) EndIf *edit: NOTE: The $binBuf can either be binary, or a binary string ("0x1A2B3C") Edited October 11, 2010 by Ascend4nt My contributions: Performance Counters in Windows - Measure CPU, Disk, Network etc Performance | Network Interface Info, Statistics, and Traffic | CPU Multi-Processor Usage w/o Performance Counters | Disk and Device Read/Write Statistics | Atom Table Functions | Process, Thread, & DLL Functions UDFs | Process CPU Usage Trackers | PE File Overlay Extraction | A3X Script Extract | File + Process Imports/Exports Information | Windows Desktop Dimmer Shade | Spotlight + Focus GUI - Highlight and Dim for Eyestrain Relief | CrossHairs (FullScreen) | Rubber-Band Boxes using GUI's (_GUIBox) | GUI Fun! | IE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) | Magnifier (Vista+) Functions UDF | _DLLStructDisplay (Debug!) | _EnumChildWindows (controls etc) | _FileFindEx | _ClipGetHTML | _ClipPutHTML + ClipPutHyperlink | _FileGetShortcutEx | _FilePropertiesDialog | I/O Port Functions | File(s) Drag & Drop | _RunWithReducedPrivileges | _ShellExecuteWithReducedPrivileges | _WinAPI_GetSystemInfo | dotNETGetVersions | Drive(s) Power Status | _WinGetDesktopHandle | _StringParseParameters | Screensaver, Sleep, Desktop Lock Disable | Full-Screen Crash Recovery Wrappers/Modifications of others' contributions: _DOSWildcardsToPCRegEx (original code: RobSaunder's) | WinGetAltTabWinList (original: Authenticity) UDF's added support/programming to: _ExplorerWinGetSelectedItems | MIDIEx UDF (original code: eynstyne) (All personal code/wrappers centrally located at Ascend4nt's AutoIT Code)
martin Posted October 12, 2010 Posted October 12, 2010 (edited) Even simpler: $hFile=FileOpen("binary.file",16) $binBuf=FileRead($hFile) $iErr=@error $iBytes=@extended FileClose($hFile) If Not $iErr Then $stStr=DllStructCreate("byte["&$iBytes&"]") DllStructSetData($stStr,1,$binBuf) EndIf *edit: NOTE: The $binBuf can either be binary, or a binary string ("0x1A2B3C") Yes you're right, it is as simple as that. Thanks Ascend4nt. Edited October 12, 2010 by martin 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.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now