LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-09-2005, 07:22 AM   #1
paddyjoy
Member
 
Registered: Apr 2005
Location: Sydney
Distribution: FC5
Posts: 174

Rep: Reputation: 30
Search file for Hex value


Does anyone know how I could search a file for an occurence of a hex value eg 0x0A0B ?

Paddy
 
Old 08-09-2005, 07:59 AM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Is the file a binary file or does the string "0x0A0B" actually exist in the file? What language are you programming in?
 
Old 08-09-2005, 11:04 AM   #3
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 215

Rep: Reputation: 31
If you just need to change that value then there is already software available. I know by the name 'hexedit'.

If you need to write the code, then jtshws's question applys.
 
Old 08-09-2005, 11:25 AM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
may be related to this post:

http://www.linuxquestions.org/questi...light=schneidz

dbx/ gdb may be able to help. i also used a post that explained how to use emacs as a hex-editor (i can't search for it now)

~schneidz
 
Old 08-09-2005, 05:15 PM   #5
paddyjoy
Member
 
Registered: Apr 2005
Location: Sydney
Distribution: FC5
Posts: 174

Original Poster
Rep: Reputation: 30
Thanks for the links it's actually a binary file. I have a way of doing it by using
Code:
hexdump <file> | grep 0x0A0B
but it's not really a great solution. I'm just trying to do this in bash but I would be interestess to know how to search through a file in C also if anyone has any tips?

Thanks,
Paddy
 
Old 08-09-2005, 09:26 PM   #6
Dave Kelly
Member
 
Registered: Aug 2004
Location: Todd Mission Texas
Distribution: Linspire
Posts: 215

Rep: Reputation: 31
Quote:
Originally posted by paddyjoy
but I would be interestess to know how to search through a file in C also if anyone has any tips?

Thanks,
Paddy [/B]
Code:
/*
**  HEXDUMP.C - Dump a file.
**
**  Originally written By Paul Edwards
**  Released to the public domain
**
**  Modified for SNIPPETS by Bob Stout
**
**  Uses ERR_EXIT.C and FERROF.C, also in SNIPPETS
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "errors.h"

static void dodump(FILE *fp, long start, long count);

main(int argc, char **argv)
{
      FILE *fp;
      long start, count, length;

      if (argc < 2)
            ErrExit("Usage: HEXDUMP file_name [start] [length]");
      if (argc > 2)
            sscanf(argv[2], "%li", &start);
      else  start = 0L;
      if (argc > 3)
            sscanf(argv[3], "%li", &count);
      else  count = -1L;

      fp = cant(argv[1], "rb");

      fseek(fp, 0L, SEEK_END);
      length = ftell(fp);
      if (start > length)
      {
            ErrExit("Can't find position %ld in a %ld byte file",
                  start, length);
      }
      if (fseek(fp, start, SEEK_SET))
            ErrExit("Unable to find position %ld", start);

      dodump(fp, start, count);
      return (EXIT_SUCCESS);
}

static void dodump(FILE *fp, long start, long count)
{
      int c, pos1, pos2, posn = (int)(start % 16L);
      long x = 0L;
      char prtln[80];

      while (((c = fgetc(fp)) != EOF) && (x != count))
      {
            if (0 == (posn % 16) || 0 == x)
            {
                  memset(prtln,' ',sizeof prtln);
                  sprintf(prtln,"%0.6X:", start + x);
                  prtln[7] = ' ';
                  pos1 = 8 + (int)(3 * posn);
                  if (posn > 3)
                        ++pos1;
                  if (posn > 7)
                        ++pos1;
                  if (posn > 11)
                        ++pos1;
                  pos2 = 60 + (int)(posn);
            }
            sprintf(prtln + pos1, "%0.2X ", c);
            if (isprint(c))
                  sprintf(prtln + pos2, "%c", c);
            else  sprintf(prtln + pos2, ".");
            pos1 += 3;
            *(prtln+pos1) = ' ';
            pos2++;
            if (posn % 4 == 3)
                  *(prtln + pos1++) = ' ';
            if (posn % 16 == 15)
            {
                  printf("%s\n", prtln);
                  posn = 0;
            }
            else  ++posn;
            ++x;
      }
      if (posn % 16)
            printf("%s\n", prtln);
      return;
}
 
Old 08-11-2005, 01:11 AM   #7
paddyjoy
Member
 
Registered: Apr 2005
Location: Sydney
Distribution: FC5
Posts: 174

Original Poster
Rep: Reputation: 30
Thanks for the code Dave I will be able to work something out with that.

Paddy
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Send an .hex file by serial port ODSunal Programming 2 12-09-2004 08:12 PM
view text file in hex under kdevelop tcma Linux - Software 1 10-20-2004 05:57 PM
Find File broken, need search utility, where does WineX install, KDE file roller? Ohmn Mandriva 6 07-05-2004 10:34 PM
Attaching an intel Hex file. krushna Linux From Scratch 0 06-07-2004 03:47 PM
How to 'apt-cache search' & 'apt-file search' by distribution? davidas Debian 3 04-19-2004 01:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration