Rules | Recent posts | topic RSS | Search | Register  | Log in

search hex values in binary file

 
Post new topic  Reply to topic    EDAboard.com Forum Index -> PC Programming and Interfacing
Author Message
aamiralikhoja



Joined: 11 Aug 2004
Posts: 88


Post28 Dec 2004 5:26   search hex values in binary file

dear fellows


I need c source code to find hexvalues in binary file if any body have

source code please help me
Back to top
echo47



Joined: 07 Apr 2002
Posts: 4206
Helped: 565


Post28 Dec 2004 5:48   search hex values in binary file

That's a little ambiguous. Do you mean the user enters an ASCII string such as "0x12 0x34 0x56" and the program searches the file for that sequence of three bytes?

Which part of that task do you need help with? Parsing the hex input string? The file open/search routine? Does it need to be blazing fast?
Back to top
aamiralikhoja



Joined: 11 Aug 2004
Posts: 88


Post28 Dec 2004 10:24   Re: search hex values in binary file

dear echo47

I want to search hex ocurecunces of hex value(not accsii string) is binary file

for example binary file has folwing format

0: 01 f0 df e9 a3 fe ff 04 00 01 85 31 30 85 34 65 43
10: 93 de af a0 43 ff 00 73 44 21 55 97 52 94 44 21 11

now if i want to find fe9a3 which occurs at first line and continue searching

the entire file and to store the position of hex value in another file how to do

that with effieient search allgo:[/i][/b][/u]
Back to top
bilgekaan



Joined: 18 Nov 2004
Posts: 118
Helped: 14
Location: Turkiye


Post28 Dec 2004 11:22   Re: search hex values in binary file

this may help you
unsigned char GetNibble( BYTE* buffer , int nibbleIndex )
{
int byteIndex = nibbleIndex / 2;

if( nibbleIndex %2 )
{
return buffer[ byteIndex ] && 0x0F;
}

return ((buffer[ byteIndex ] && 0xF0)>>4);

}

unsigned char databuffer[ BUFFER_SIZE ];

unsigned char nibble_pattern[] = { 0xf , 0x0e , 0x9 , 0xa , 0x3 };

int num_nibbles = 5;

bool FindFirst( )
{

for( int i = 0 ; i < BUFFER_SIZE * 2 - num_nibbles ; i ++ )
{

if( GetNibble( databuffer , i ) == nibble_pattern[1]&&
GetNibble( databuffer , i + 1 ) == nibble_pattern[0]&&
GetNibble( databuffer , i + 2 ) == nibble_pattern[3]&&
GetNibble( databuffer , i + 3 ) == nibble_pattern[2]&&
GetNibble( databuffer , i + 4 ) == nibble_pattern[4]
)

{
return true;
}
}

return false;
}
Back to top
Always_Confused_Sudent



Joined: 26 Dec 2003
Posts: 29


Post28 Dec 2004 12:15   Re: search hex values in binary file

bilgekaan wrote:
this may help you
unsigned char GetNibble( BYTE* buffer , int nibbleIndex )
{
int byteIndex = nibbleIndex / 2;

if( nibbleIndex %2 )
{
return buffer[ byteIndex ] && 0x0F;
}

return ((buffer[ byteIndex ] && 0xF0)>>4);

}

unsigned char databuffer[ BUFFER_SIZE ];

unsigned char nibble_pattern[] = { 0xf , 0x0e , 0x9 , 0xa , 0x3 };

int num_nibbles = 5;

bool FindFirst( )
{

for( int i = 0 ; i < BUFFER_SIZE * 2 - num_nibbles ; i ++ )
{

if( GetNibble( databuffer , i ) == nibble_pattern[1]&&
GetNibble( databuffer , i + 1 ) == nibble_pattern[0]&&
GetNibble( databuffer , i + 2 ) == nibble_pattern[3]&&
GetNibble( databuffer , i + 3 ) == nibble_pattern[2]&&
GetNibble( databuffer , i + 4 ) == nibble_pattern[4]
)

{
return true;
}
}

return false;
}


can you explain to me why unsigned char was used to declare the function
Back to top
bilgekaan



Joined: 18 Nov 2004
Posts: 118
Helped: 14
Location: Turkiye


Post28 Dec 2004 13:01   Re: search hex values in binary file

unsigned char is same as BYTE . databuffer is used to store binary file content.

Added after 28 minutes:

[quote="Always_Confused_Sudent"]
bilgekaan wrote:
this may help you
unsigned char GetNibble( BYTE* buffer , int nibbleIndex )
{
int byteIndex = nibbleIndex / 2;

if( nibbleIndex %2 )
{
return buffer[ byteIndex ] && 0x0F;
}

return ((buffer[ byteIndex ] && 0xF0)>>4);

}

unsigned char databuffer[ BUFFER_SIZE ];

unsigned char nibble_pattern[] = { 0xf , 0x0e , 0x9 , 0xa , 0x3 };

int num_nibbles = 5;

bool FindFirst( )
{

for( int i = 0 ; i < BUFFER_SIZE * 2 - num_nibbles ; i ++ )
{

if( GetNibble( databuffer , i ) == nibble_pattern[1]&&
GetNibble( databuffer , i + 1 ) == nibble_pattern[0]&&
GetNibble( databuffer , i + 2 ) == nibble_pattern[3]&&
GetNibble( databuffer , i + 3 ) == nibble_pattern[2]&&
GetNibble( databuffer , i + 4 ) == nibble_pattern[4]
)

{
return true;
}
}

return false;
}


this function an prototype and never tested written in minutes. unsigned char is equal to BYTE . replace all with one of two to make code readable. I am sure there is also indexing error to access nibbles .please check
Back to top
Always_Confused_Sudent



Joined: 26 Dec 2003
Posts: 29


Post28 Dec 2004 18:58   Re: search hex values in binary file

is it equal to BYTE in all compilers?
Back to top
bilgekaan



Joined: 18 Nov 2004
Posts: 118
Helped: 14
Location: Turkiye


Post28 Dec 2004 20:06   Re: search hex values in binary file

yes for most of them . but also there is unicode version of char which is 16 bit

used to represent different alphabets using different letters like turksih , chinese etc
Back to top
Post new topic  Reply to topic    EDAboard.com Forum Index -> PC Programming and Interfacing
Page 1 of 1 All times are GMT + 1 Hour


Abuse
Administrator
Moderators
topic RSS 
sitemap