Count pattern occurrences without storing positions (memory efficient).
CountOccurrences() counts how many times a byte pattern appears in the file without storing positions, making it much more memory-efficient than FindAll().Count() for large files.
public int CountOccurrences(byte[] pattern, long startPosition = 0)Since: V2.0
| Parameter | Type | Description | Default |
|---|---|---|---|
pattern |
byte[] |
Byte pattern to count | - |
startPosition |
long |
Position to start from | 0 |
| Type | Description |
|---|---|
int |
Number of occurrences found |
// Count how many times 0xFF appears
var pattern = new byte[] { 0xFF };
int count = hexEditor.CountOccurrences(pattern);
Console.WriteLine($"Found {count} occurrences of 0xFF");// Count null-terminated strings
var nullTerm = new byte[] { 0x00 };
int stringCount = hexEditor.CountOccurrences(nullTerm);
// Count specific signature
var signature = new byte[] { 0xDE, 0xAD, 0xBE, 0xEF };
int signatureCount = hexEditor.CountOccurrences(signature);// Count occurrences after position 0x1000
var pattern = new byte[] { 0xAA, 0xBB };
int count = hexEditor.CountOccurrences(pattern, startPosition: 0x1000);| File Size | Matches | FindAll().Count() Memory |
CountOccurrences() Memory |
|---|---|---|---|
| 10 MB | 1,000 | ~8 KB | ~0 KB |
| 100 MB | 10,000 | ~80 KB | ~0 KB |
| 1 GB | 100,000 | ~800 KB | ~0 KB |
| Scenario | Use CountOccurrences() |
Use FindAll() |
|---|---|---|
| Only need count | ✅ Yes | ❌ No |
| Need positions | ❌ No | ✅ Yes |
| Large file | ✅ Yes | |
| Many matches | ✅ Yes |
// Analyze byte frequency
int nullBytes = hexEditor.CountOccurrences(new byte[] { 0x00 });
int ffBytes = hexEditor.CountOccurrences(new byte[] { 0xFF });
Console.WriteLine($"File contains {nullBytes} null bytes and {ffBytes} FF bytes");// Check if pattern exists before processing
var pattern = new byte[] { 0x4D, 0x5A }; // MZ header
int count = hexEditor.CountOccurrences(pattern);
if (count == 0)
{
Console.WriteLine("No PE executables found");
}
else
{
Console.WriteLine($"Found {count} potential PE executables");
}// Count total before processing
var pattern = new byte[] { 0xAA, 0xBB };
int total = hexEditor.CountOccurrences(pattern);
// Now process each (using FindAll for positions)
int processed = 0;
foreach (var pos in hexEditor.FindAll(pattern))
{
// Process...
processed++;
Console.WriteLine($"Progress: {processed}/{total}");
}- FindAll() - Find all positions (stores in memory)
- FindFirst() - Find first occurrence
- ReplaceAll() - Replace all occurrences
Last Updated: 2026-02-19