Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

search hex values in binary file

Status
Not open for further replies.

aamiralikhoja

Member level 5
Member level 5
Joined
Aug 11, 2004
Messages
90
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
708
dear fellows


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

source code please help me
 

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?
 

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]
 

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;
}
 

bilgekaan said:
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
 

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

Added after 28 minutes:

Always_Confused_Sudent said:
bilgekaan said:
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
 

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
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top