say I have the below binary file name test.bin. I want to count the occurrence of the same bytes [44 04 85 44 10 10 01 01 10 00 08] in that file. how do I do that? The answer should be 3. Some sample could be be greatly appreciated. thanks.
44 04 85 44 10 10 01 01 10 00 08 00 44 17 44 62
00 00 a0 ff ff fb 1c 41 64 d1 a7 0f 0f 00 00 20
e9 00 01 b0 00 00 02 00 02 f0 43 7a e4 87 a5 59
b0 75 00 c3 50 1b b7 0e 89 04 d2 1f ff 01 a0 ff
ff fb 1c 41 64 5b a7 0f 0f 00 00 20 ee 00 01 af
00 00 02 00 02 bc ac 08 69 6b eb 03 3c 00 0f b9
00 00 00 04 00 00 00 00 04 00 00 55 55 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 01 ae 32 30 32 30 32 38 38 33
30 30 30 30 30 30 30 30 30 31 30 30 30 30 30 30
30 30 44 04 85 44 10 10 01 01 10 00 08 00 44 17
44 62 00 00 a0 ff ff f6 1c 41 66 2f a7 0f 0f 00
00 20 d5 00 01 b2 00 00 02 00 02 f2 cf 24 98 19
9f 1b 3c 75 00 c3 50 1b b7 0e 89 04 d2 1f ff 01
a0 ff ff f6 1c 41 66 04 a7 0f 0f 00 00 20 df 00
01 b1 00 00 02 00 02 b8 ff 77 5e 51 95 fb 74 00
0f b9 00 00 00 04 00 00 00 00 04 00 00 55 55 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 01 b0 32 30 32 30 32 38
38 33 30 30 30 30 30 30 30 30 30 31 30 30 30 30
30 30 30 30 44 04 85 44 10 10 01 01 10 00 08 00
44 17 44 62 00 00 a0 ff ff f1 1c 41 68 2d a7 0f
0f 00 00 20 c6 00 01 b3 00 00 02 00 02 fc a4 04
75 1a ca fc 00 75 00 c3 50 1b b7 0e 89 04 d2 1f
ff 01 a0 ff ff f6 1c 41 66 2f a7 0f 0f 00 00 20
d5 00 01 b2 00 00 02 00 02 f2 cf 24 98 19 9f 1b
3c 00 0f b9 00 00 00 04 00 00 00 00 04 00 00 55
55 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 01 b1 32 30 32 30
32 38 38 33 30 30 30 30 30 30 30 30 30 31 30 30
30 30 30 30 30 30

1 answers
I'm assuming that the above is a hex dump of the bytes in the file, not how the file actually looks :)
If speed is not an issue, then an easy way to do this is to use regular expressions. The only problem is that the .NET implementation of Regex can't search byte arrays directly - you have to convert them, and the expression to search for, to unicode strings.
Anyway, here's some code:
EDIT
Here's a more efficient version which converts the bytes to a unicode string as it reads them from the file using code page 28591. The latter, which represents the 8 bit ISO-8859-1 character set (or Latin-1), shares the same characters as unicode in the range 0-255 which, of course, is the same range as a byte.
answered 2 years ago by:
17279
494
thank you, it works. Wonderful.