Question on Preg_match_all and hex pattern finding

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kurbot
Forum Newbie
Posts: 18
Joined: Tue Jul 20, 2010 2:20 pm

Question on Preg_match_all and hex pattern finding

Post by kurbot »

Question for you guys.. This question is going to be hard to explain but lets see how well i do.

Im working on debugging Hex, and ive been able to convert a file uploaded into hex completely, what im trying to do now is run through the hex code and find a pattern. Ive gotten it to work for 00 - 19 but im trying to get the 0a -1f to work too.. meaning 00-19 or 0a-1f

What im trying to do after i find the pattern in the hex is to break it into an array..

The pattern im looking of is ##,00,00,00,## ( 00,00,00,00 -> 1f,00,00,00,1f )

example would be 02,00,00,00,02 OR 02,00,00,0a OR 0a,00,00,00,0f

The # sign being 00 - 19 or 0a - 1f .. Really all i need to gather is the following array.. cause the numbers im looking for are under 30 decimal. or 1F hex

what i currently am using to get the first half is this:

Code: Select all

preg_match_all("/(\d{1,2}),00,00,00,(\d{1,2})/", $string, $hexFind); 
	
	$hexMatch = array('00' => '0',
				  '01' => '1',
				  '02' => '2',
				  '03' => '3',
				  '04' => '4',
				  '05' => '5',
				  '06' => '6',
				  '07' => '7',
				  '08' => '8',
				  '09' => '9',
				  '0A' => '10',
				  '0B' => '11',
				  '0C' => '12',
				  '0D' => '13',
				  '0E' => '14',
				  '0F' => '15',
				  '10' => '16',
				  '12' => '17',
				  '13' => '18',
				  '14' => '19',
				  '15' => '20',
				  '16' => '21',
				  '17' => '22',
				  '18' => '23',
				  '19' => '24',
				  '1A' => '25',
				  '1B' => '26',
				  '1C' => '27',
				  '1D' => '28',
				  '1E' => '29',
				  '1F' => '30'
				  
				  );
	for ($i = 0; $i < count($hexFind[1]); $i++){
		if($hexMatch[$hexFind[1][$i]] != '' && $hexMatch[$hexFind[2][$i]] != ''){
			$currenCol['X'] = $hexM[$hexFind[1][$i]];
			$currenCol['Y'] = $hexM[$hexFind[2][$i]];
			echo $currenCol['X'].','.$currenCol['Y'].'<br>';
			$data[] = $currenCol['X'].','.$currenCol['Y'];
		}
	}
Let me know if you could help me adjust the preg_match_all to find both numbers and alpa within the array range.

Thanks ahead of time.

-Dave
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Question on Preg_match_all and hex pattern finding

Post by requinix »

1. Sanity check: 30 decimal != 1F hexadecimal.

Code: Select all

'10' => '16',
'12' => '17',
2. Get rid of the whole $hexMatch array. It's a bad way of doing something that you don't even need to do.
3. Your regular expression will only match numbers. If you want it to match hexadecimal digits then you have to fix that first.
4. Are the A-F digits in uppercase or lowercase? Always?
5. Because digits come before letters in ASCII too, you can compare two hexadecimal strings of the same length just like you would compare regular decimal numbers. Yes, that means "09" < "0A" < "0F" < "10".

Oh, and by the way:

Code: Select all

preg_match_all("/([01].),00,00,00,([01].)/", $string, $hexFind);
Regular expression that only matches 00-1F,00,00,00,00-1F.
Post Reply