Is possible to use regex to read binary file?

  • Hello everyone,


    I have the code below to read a binary file one byte at a time.


    The issue is the binary is divided by blocks from that I would like to get some patterns from each block of data and print the
    data for each block in a single row. Each block begins with ff77, so initially I would like to know if it is possible to use Regex
    to use ff77 as line separator and read one block at a time and print first block in row 1, block 2 in row2 and so on, to avoid
    overload the buffer.


    If regexp could be used, maybe it could be possible to get some sequences from each block.


    This is the code I have so far (binary.txt attached).


    Thanks in advance for any help.

  • Re: Is possible to use regex to read binary file?


    Hi
    You code just loop and read each line


    Try.....

  • Re: Is possible to use regex to read binary file?


    I am not sure if mine is wrong and if so where i went wrong.. but my output data is very different to pikes.. (theres heaps more output data)


    I am guessing pikes is correct though...


    I'll post it just in case by a miracle mine is what you want.. :)

  • Re: Is possible to use regex to read binary file?


    If each block contain 20 values then


    or you can have your code read every 20 or block of values

  • Re: Is possible to use regex to read binary file?


    Hello pike and apo,

    Thank you for your help. I've trying your codes and it seems separate each block.

    But each block have variable length, any of them have only 20 bytes. For example, 1rst block has 312 bytes, 2nd block 435 bytes,
    3rd block 377 bytes, 4th block 386 bytes and 5th block 299 bytes.

    I'm attaching the text file with each block and the zeros at the end that are used to fill only. It seems apo´s code prints completely
    each block, except that if one byte is "00" is only printing one zero. I don't know how to avoid that VBA prints only one zero in those
    cases.

    With pike's code if I change the loop from 1 to 300 it seems to print all the block in some cases, but it seems to print more or less
    bytes too in other cases.

    Besides that, I would like to know if it is possible to load in buffer not the complete file, but the each block at a time or for example
    4 KB at a time in order to avoid overload? because the file could more than 2GB.

    Thanks again for the help.

  • Re: Is possible to use regex to read binary file?


  • Re: Is possible to use regex to read binary file?


    if there are different block types other than FF 77


  • Re: Is possible to use regex to read binary file?


    Hi..


    Regarding..

    Quote

    Besides that, I would like to know if it is possible to load in buffer not the complete file, but the each block at a time or for example
    4 KB at a time in order to avoid overload? because the file could more than 2GB.


    I have very limited knowledge in regards to this.. but from what i have read today (thanks to your post).. maybe some of the following 'ideas' might work..


    * Read the text file line by line instead of putting the whole text file into memory..
    * I am thinking doing it line by line will make it hard to form your 'blocks' of data.. maybe you can write each line into an array as it comes and when the identifier (FF77) is read.. dump the array into the cell and keep going through your text file (line by line).. rebuilding your array ready for the next 'dump'..


    It kinda sounds feasible to me.. anyone else got thoughts on this part of the issue?

  • Re: Is possible to use regex to read binary file?


    Quote from pike;685660

    Hi the start of the block is the pair FF and 77
    what indicates the end of the block?
    what is the desired out put?


    Hello pike/apo/snb,


    Thanks so much for the help. I've tested all your codes, and snb's code it seems to print all content of each block, the only thing is that each block not always ends with CC0101. I think the only rule to separate blocks should be FF77.


    Actually, to be precise, each block begins with 77 and ends with FF but could be many FF inside each block, and each block
    has variable length and not always ends with CC0101, but the FF follow by 77(FF77) separate 2 blocks. Due to that I mentioned
    how to put FF77 as line (block) separator (like \r\n is line separator in a text file).


    I've mentioned in my first post initially I'd like to know how to separate each block an store it in a variable and then how
    to use Regex to extract some patterns from each block. For example, second block contains the following string and once
    I could have each block I would like to get the sequences in color.


    FF7700000253206445018934551F81474554768FFFFF0015000A4800015A00024200016000013300013600013700015B00016600016500017700017800017E00
    016900006A00007900009300012200002100010900010A00012600010200010400010500010600011000010800012B00002C00012D00012E0001550001560007
    2A00002F0000300000310000FF7900800932C90688888000A000800935C90600008000000080093CC906888880008000800943C90688888000800005900F0102
    000000308147526905FFFFFF00910F01020000013A81475269559FFFFF009310010C0000009F8147526905FFFFFF0101960F010E000000EB81475269596FFFFF
    00970F01010006F69981475269563FFFFF00940E0001000001000100FFFF00000101
    95060003790001EA05820037060100000100650000000200000200180000
    000300000300170000000400000400010000000500000500150000000A00FFFF006500000007802EC918009181475269539FFFFFFF009181475269539FFFFF00
    0103CA030808FECB0A00000000000000000000CC0101811BC90B009181475269567FFFFFFFCA06000000000000CB0103CC0101


    For this purpose I have the following 2 regex that work for each block, but I don't know how to apply it in the code.

    Code
    ^(.{6})(532064[^f]*).(814[^f]*)
    05(9.{32,34}.*?)940e(.{28})


    Thanks again for the help.

  • Re: Is possible to use regex to read binary file?


    Now you lost me.


    Before writing results into the worksheet, all blocks are being stored in an array-variable sn.
    Each element of that array variable contains exactly 1 block.
    What more/else do you want ?
    Can you describe in plain Englisch what are the criteria to select anything from each block ?

  • Re: Is possible to use regex to read binary file?


    Hello pike,


    In this case the substrings that I want to extract don't appear in the same position from file to another. The most direct way to get them I can think is by regex. Actually, if each block can be stored in a variable, I think could be possible to extract the strings with those 2 regex.


    The 3 values after FF77 are always in the same position, but the long string in red don't have a fixed position. Is what I know from file structure.


    Thanks for the help

  • Re: Is possible to use regex to read binary file?


    Quote from snb;685779

    Now you lost me.


    Before writing results into the worksheet, all blocks are being stored in an array-variable sn.
    Each element of that array variable contains exactly 1 block.
    What more/else do you want ?
    Can you describe in plain Englisch what are the criteria to select anything from each block ?


    Hello snb,


    The 1st step to get my goal is have each block in a string. In your code is in "sn" variable, I was trying to removes
    the spaces from "sn" variable, but I'm still failing in that task. Maybe you can help me with that.


    The 2nd step is be able to apply regex to "sn" variable in order to store in "sn1" the content extracted for
    first regex (pattern1).

    Code
    ^(.{6})(532064[^f]*).(814[^f]*)


    and store in "sn2" the content extracted with second regex (if exists, since pattern2 not always exists).

    Code
    05(9.{32,34}.*?)940e(.{28})


    Finally I'll need to do a further processing over pattern2 stored in "sn2" before printing.


    I hope make sense.


    Hello pike,


    Quote from pike

    what type of file?
    what is the relivance of the data you are reteving?


    Sorry. I don't understand the question.


    Thanks again for the help.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!