Jump to content

How to put a binary string into an array?


Recommended Posts

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 by Edward Mendelson
Link to comment
Share on other sites

$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 by Ascend4nt
Link to comment
Share on other sites

$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.
Link to comment
Share on other sites

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 by Ascend4nt
Link to comment
Share on other sites

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 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.
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...