Compare char[2] with short

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • spasmous

    Compare char[2] with short

    I need a way to search through a block of memory for a char[2] array
    "DA" using a pointer to a short. Ideally I would like to write
    something like:

    short *data = ... some data...;
    int j = 0;
    while( data[j] != *((short*) "DA") ) j++;

    But this doesn't work. The char[2] obviously has an equivalent 16-bit
    value so how do I get that info in a simple way?
  • Ian Collins

    #2
    Re: Compare char[2] with short

    spasmous wrote:
    I need a way to search through a block of memory for a char[2] array
    "DA" using a pointer to a short. Ideally I would like to write
    something like:
    >
    short *data = ... some data...;
    int j = 0;
    while( data[j] != *((short*) "DA") ) j++;
    >
    But this doesn't work. The char[2] obviously has an equivalent 16-bit
    value so how do I get that info in a simple way?
    Just compare character by character.

    while( data[j] != 'D' && data[j+1] != 'A') ) j++;

    Don't forget to add checking for the end of the array!

    --
    Ian Collins.

    Comment

    • spasmous

      #3
      Re: Compare char[2] with short

      On May 17, 1:13 pm, Ian Collins <ian-n...@hotmail.co mwrote:
      spasmouswrote:
      I need a way to search through a block of memory for a char[2] array
      "DA" using a pointer to a short. Ideally I would like to write
      something like:
      >
      short *data = ... some data...;
      int j = 0;
      while( data[j] != *((short*) "DA") ) j++;
      >
      But this doesn't work. The char[2] obviously has an equivalent 16-bit
      value so how do I get that info in a simple way?
      >
      Just compare character by character.
      >
      while( data[j] != 'D' && data[j+1] != 'A') ) j++;
      >
      Don't forget to add checking for the end of the array!
      >
      Maybe I'm mixed up, but at least on my platform char is 8-bit and
      short is 16-bit. So one data[j] is the same as two chars.

      Comment

      • Lew Pitcher

        #4
        Re: Compare char[2] with short

        In comp.lang.c, spasmous wrote:
        I need a way to search through a block of memory for a char[2] array
        "DA" using a pointer to a short. Ideally I would like to write
        something like:
        >
        short *data = ... some data...;
        int j = 0;
        while( data[j] != *((short*) "DA") ) j++;
        >
        But this doesn't work. The char[2] obviously has an equivalent 16-bit
        value so how do I get that info in a simple way?
        An implementation specific[1] (but still, IIRC, legal in standard C) way
        would be to

        while (data[j] != 'DA') ++j;


        [1] ISO C 9899-1999 6.4.4.4. Point 10 (Semantics) says "The value of an
        integer character constant containing more than one character ... is
        implementation-defined." 'DA' is such a constant.

        --
        Lew Pitcher

        Master Codewright & JOAT-in-training | Registered Linux User #112576
        http://pitcher.digitalfreehold.ca/ | GPG public key available by request
        ---------- Slackware - Because I know what I'm doing. ------


        Comment

        • Ian Collins

          #5
          Re: Compare char[2] with short

          spasmous wrote:
          On May 17, 1:13 pm, Ian Collins <ian-n...@hotmail.co mwrote:
          >spasmouswrot e:
          >>I need a way to search through a block of memory for a char[2] array
          >>"DA" using a pointer to a short. Ideally I would like to write
          >>something like:
          >>short *data = ... some data...;
          >>int j = 0;
          >>while( data[j] != *((short*) "DA") ) j++;
          >>But this doesn't work. The char[2] obviously has an equivalent 16-bit
          >>value so how do I get that info in a simple way?
          >Just compare character by character.
          >>
          >while( data[j] != 'D' && data[j+1] != 'A') ) j++;
          >>
          >Don't forget to add checking for the end of the array!
          >>
          >
          Maybe I'm mixed up, but at least on my platform char is 8-bit and
          short is 16-bit. So one data[j] is the same as two chars.
          Oops, I didn't spot that data was short.

          const char* p = (const char*)data;
          const char* end = p+lengthOfData* 2;

          while( p!= end && *p != 'D' && *(p+1) != 'A') ) p+=2;

          --
          Ian Collins.

          Comment

          • Antoninus Twink

            #6
            Re: Compare char[2] with short

            On 17 May 2008 at 21:07, spasmous wrote:
            I need a way to search through a block of memory for a char[2] array
            "DA" using a pointer to a short. Ideally I would like to write
            something like:
            >
            short *data = ... some data...;
            int j = 0;
            while( data[j] != *((short*) "DA") ) j++;
            >
            But this doesn't work.
            It obviously won't work if the occurence of DA in your data isn't
            aligned at a 16-bit boundary:

            short *x=(short *) "Hello DAMN you"; /* OK: x[3] is DA */
            short *x=(short *) "Hello, DAMN you"; /* oh dear... */
            The char[2] obviously has an equivalent 16-bit value so how do I get
            that info in a simple way?
            What you have, i.e. *((short*) "DA"), will do that just fine. You can
            also do something like 'D'+('A' << 8), but that relies on your machine
            being little endian...

            Comment

            • Walter Roberson

              #7
              Re: Compare char[2] with short

              In article <154484e5-7e8d-4c99-a58a-4a2e087f4990@t1 2g2000prg.googl egroups.com>,
              spasmous <spasmous@gmail .comwrote:
              >I need a way to search through a block of memory for a char[2] array
              >"DA" using a pointer to a short. Ideally I would like to write
              >something like:
              >short *data = ... some data...;
              >int j = 0;
              >while( data[j] != *((short*) "DA") ) j++;
              >But this doesn't work. The char[2] obviously has an equivalent 16-bit
              >value so how do I get that info in a simple way?
              Can the constant occur on an odd boundary? e.g., if the array had

              XDAYDA

              then would you want the match at character offset 1, or the one
              at character offset 4?

              If you need to find the ones on odd boundaries but were hoping to
              compare characters two at a time instead of one at a time, then
              you will not be able to do it simply by incrementing a byte at a time
              but comparing two characters as a short, as many systems have alignment
              requirements that do not allow shorts to be located on odd byte boundaries.

              If you do want to search on odd boundaries, then here is a simple
              algorithm that can make it more efficient:

              unsigned char *datachar = (char *) data;
              size_t j = 0, maxj = SIZE_OF_DATA_AR RAY;
              while (j < maxj-1) {
              if (datachar[j+1] == 'A') {
              if (datachar[j] == 'D')
              break;
              } else if (datachar[j+1] == 'D') {
              j++;
              } else {
              j += 2;
              }
              }

              That is, if the next character ahead is not an 'A', there is no
              point in checking the current character for 'D' -- either it
              isn't a 'D' at all, or it isn't a "useful" 'D' because it isn't
              followed by 'A'.
              --
              "The whole history of civilization is strewn with creeds and
              institutions which were invaluable at first, and deadly
              afterwards." -- Walter Bagehot

              Comment

              • Ben Bacarisse

                #8
                Re: Compare char[2] with short

                spasmous <spasmous@gmail .comwrites:
                I need a way to search through a block of memory for a char[2] array
                "DA" using a pointer to a short. Ideally I would like to write
                something like:
                >
                short *data = ... some data...;
                int j = 0;
                while( data[j] != *((short*) "DA") ) j++;
                >
                But this doesn't work. The char[2] obviously has an equivalent 16-bit
                value so how do I get that info in a simple way?
                It might be better to post a minimal example that illustrates the
                way in which this does not work because, for certain values of
                "work", the above code does do what you want.

                The most portable solution will a string-based one. There is a good
                chance it will also be much faster than you fear it will be (I suspect
                you want to use a short * for fear of a slow character-based search).
                For example:

                char *found, *look = raw_data;
                while ((found = strstr(look, "DA")) != NULL &&
                (found - raw_data) % 2 == 1)
                look = found + 1;
                if (found)
                /* Ah! there you are */

                --
                Ben.

                Comment

                • Keith Thompson

                  #9
                  Re: Compare char[2] with short

                  Antoninus Twink <nospam@nospam. invalidwrites:
                  On 17 May 2008 at 21:07, spasmous wrote:
                  >I need a way to search through a block of memory for a char[2] array
                  >"DA" using a pointer to a short. Ideally I would like to write
                  >something like:
                  >>
                  >short *data = ... some data...;
                  >int j = 0;
                  >while( data[j] != *((short*) "DA") ) j++;
                  >>
                  >But this doesn't work.
                  >
                  It obviously won't work if the occurence of DA in your data isn't
                  aligned at a 16-bit boundary:
                  >
                  short *x=(short *) "Hello DAMN you"; /* OK: x[3] is DA */
                  short *x=(short *) "Hello, DAMN you"; /* oh dear... */
                  Mr. "Twink" needs to re-tune his concept of what's "obvious".

                  The language doesn't require that char is 8 bits, that short is 16
                  bits, or that short has any particular alignment requirement. For
                  example, on some very popular platforms, accessing a 2-byte quantity
                  on an odd byte address works just fine (though it's slightly slower
                  than accessing something on an even address).

                  If by "won't work" he means that the behavior is undefined, he has a
                  point, but one possible, and in this case very plausible, consequence
                  of undefined behavior is that it "works".
                  >The char[2] obviously has an equivalent 16-bit value so how do I get
                  >that info in a simple way?
                  >
                  What you have, i.e. *((short*) "DA"), will do that just fine. You can
                  also do something like 'D'+('A' << 8), but that relies on your machine
                  being little endian...
                  There's no guarantee that the array associated with the string literal
                  "DA" is appropriately aligned for a short. It's likely to be
                  adequately aligned, but I wouldn't depend on it.

                  --
                  Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                  Nokia
                  "We must do something. This is something. Therefore, we must do this."
                  -- Antony Jay and Jonathan Lynn, "Yes Minister"

                  Comment

                  • Keith Thompson

                    #10
                    Re: Compare char[2] with short

                    spasmous <spasmous@gmail .comwrites:
                    I need a way to search through a block of memory for a char[2] array
                    "DA" using a pointer to a short. Ideally I would like to write
                    something like:
                    >
                    short *data = ... some data...;
                    int j = 0;
                    while( data[j] != *((short*) "DA") ) j++;
                    >
                    But this doesn't work. The char[2] obviously has an equivalent 16-bit
                    value so how do I get that info in a simple way?
                    Consider treating the short array as an array of char and using
                    strstr().

                    --
                    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                    Nokia
                    "We must do something. This is something. Therefore, we must do this."
                    -- Antony Jay and Jonathan Lynn, "Yes Minister"

                    Comment

                    • Jack Klein

                      #11
                      Re: Compare char[2] with short

                      On Sat, 17 May 2008 17:24:14 -0400, Lew Pitcher
                      <lpitcher@teksa vvy.comwrote in comp.lang.c:
                      In comp.lang.c, spasmous wrote:
                      >
                      I need a way to search through a block of memory for a char[2] array
                      "DA" using a pointer to a short. Ideally I would like to write
                      something like:

                      short *data = ... some data...;
                      int j = 0;
                      while( data[j] != *((short*) "DA") ) j++;

                      But this doesn't work. The char[2] obviously has an equivalent 16-bit
                      value so how do I get that info in a simple way?
                      >
                      An implementation specific[1] (but still, IIRC, legal in standard C) way
                      would be to
                      >
                      while (data[j] != 'DA') ++j;
                      >
                      >
                      [1] ISO C 9899-1999 6.4.4.4. Point 10 (Semantics) says "The value of an
                      integer character constant containing more than one character ... is
                      implementation-defined." 'DA' is such a constant.
                      Legal, yes, but of very doubtful usefulness. There is no guarantee,
                      or even much reason to believe, that 'DA' will look anything like "DA"
                      in memory.

                      --
                      Jack Klein
                      Home: http://JK-Technology.Com
                      FAQs for
                      comp.lang.c http://c-faq.com/
                      comp.lang.c++ http://www.parashift.com/c++-faq-lite/
                      alt.comp.lang.l earn.c-c++

                      Comment

                      • Ike Naar

                        #12
                        Re: Compare char[2] with short

                        In article <154484e5-7e8d-4c99-a58a-4a2e087f4990@t1 2g2000prg.googl egroups.com>,
                        spasmous <spasmous@gmail .comwrote:
                        >I need a way to search through a block of memory for a char[2] array
                        >"DA" using a pointer to a short. Ideally I would like to write
                        >something like:
                        >
                        >short *data = ... some data...;
                        >int j = 0;
                        >while( data[j] != *((short*) "DA") ) j++;
                        >
                        >But this doesn't work. The char[2] obviously has an equivalent 16-bit
                        >value so how do I get that info in a simple way?
                        Perhaps you could try something like:

                        #include <string.h/* for memcpy */

                        short pattern;
                        memcpy(&pattern , "DA", 2);
                        while (data[j] != pattern) j++;

                        Ike

                        Comment

                        • Keith Thompson

                          #13
                          Re: Compare char[2] with short

                          Keith Thompson <kst-u@mib.orgwrites :
                          spasmous <spasmous@gmail .comwrites:
                          >I need a way to search through a block of memory for a char[2] array
                          >"DA" using a pointer to a short. Ideally I would like to write
                          >something like:
                          >>
                          >short *data = ... some data...;
                          >int j = 0;
                          >while( data[j] != *((short*) "DA") ) j++;
                          >>
                          >But this doesn't work. The char[2] obviously has an equivalent 16-bit
                          >value so how do I get that info in a simple way?
                          >
                          Consider treating the short array as an array of char and using
                          strstr().
                          Which won't work if the short array, treated as an array of char, has
                          any 0 bytes before the pattern you're looking for, and will yield
                          false positives if the pattern "DA" crosses a boundary. Oh, well.

                          --
                          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                          Nokia
                          "We must do something. This is something. Therefore, we must do this."
                          -- Antony Jay and Jonathan Lynn, "Yes Minister"

                          Comment

                          • Antoninus Twink

                            #14
                            Re: Compare char[2] with short

                            On 17 May 2008 at 22:04, Keith Thompson wrote:
                            Antoninus Twink <nospam@nospam. invalidwrites:
                            >It obviously won't work if the occurence of DA in your data isn't
                            >aligned at a 16-bit boundary:
                            >>
                            > short *x=(short *) "Hello DAMN you"; /* OK: x[3] is DA */
                            > short *x=(short *) "Hello, DAMN you"; /* oh dear... */
                            >
                            Mr. "Twink" needs to re-tune his concept of what's "obvious".
                            >
                            The language doesn't require that char is 8 bits, that short is 16
                            bits, or that short has any particular alignment requirement. For
                            example, on some very popular platforms, accessing a 2-byte quantity
                            on an odd byte address works just fine (though it's slightly slower
                            than accessing something on an even address).
                            It's implicit in the OP's statement of the problem that on his system,
                            char is 8 bits and short is 16 bits.

                            My point was that whether the 2-byte quantities are at odd or even byte
                            addresses, it's still possible for DA to be missed by treating each pair
                            of bytes as a short: in the second example above, " D" and "AM" will
                            occur as shorts, but "DA" won't.

                            Comment

                            • CBFalconer

                              #15
                              Re: Compare char[2] with short

                              Ike Naar wrote:
                              spasmous <spasmous@gmail .comwrote:
                              >
                              >I need a way to search through a block of memory for a char[2]
                              >array "DA" using a pointer to a short. Ideally I would like to
                              >write something like:
                              >>
                              > short *data = ... some data...;
                              > int j = 0;
                              > while( data[j] != *((short*) "DA") ) j++;
                              >>
                              >But this doesn't work. The char[2] obviously has an equivalent
                              >16-bit value so how do I get that info in a simple way?
                              >
                              Perhaps you could try something like:
                              >
                              #include <string.h/* for memcpy */
                              >
                              short pattern;
                              memcpy(&pattern , "DA", 2);
                              while (data[j] != pattern) j++;
                              Try this (or a modification):

                              /*
                              Leor Zolman wrote:
                              On 25 Feb 2004 07:34:40 -0800, joan@ljungh.se (spike) wrote:
                              >
                              >Im trying to write a program that should read through a binary
                              >file searching for the character sequence "\name\"
                              >>
                              >Then it should read the characters following the "\name\"
                              >sequence until a NULL character is encountered.
                              >>
                              >But when my program runs it gets a SIGSEGV (Segmentation
                              >vioalation) signal.
                              >>
                              >Whats wrong? And is there a better way than mine to solve
                              >>this task (most likely)
                              >
                              I think so. Here's a version I just threw together:
                              */

                              /* And heres another throw -- binfsrch.c by CBF */
                              #include <stdio.h>
                              #include <stdlib.h>
                              #include <string.h>
                              #include <ctype.h>
                              #include <assert.h>

                              /* The difference between a binary and a text file, on read,
                              is the conversion of end-of-line delimiters. What those
                              delimiters are does not affect the action. In some cases
                              the presence of 0x1a EOF markers (MsDos) does.

                              This is a version of Knuth-Morris-Pratt algorithm. The
                              point of using this is to avoid any backtracking in file
                              reading, and thus avoiding any use of buffer arrays.
                              */

                              size_t chrcount; /* debuggery, count of input chars, zeroed */

                              /* --------------------- */

                              /* Almost straight out of Sedgewick */
                              /* The next array indicates what index in id should next be
                              compared to the current char. Once the (lgh - 1)th char
                              has been successfully compared, the id has been found.
                              The array is formed by comparing id to itself. */
                              void initnext(int *next, const char *id, int lgh)
                              {
                              int i, j;

                              assert(lgh 0);
                              next[0] = -1; i = 0; j = -1;
                              while (i < lgh) {
                              while ((j >= 0) && (id[i] != id[j])) j = next[j];
                              i++; j++;
                              next[i] = j;
                              }
                              #ifdef DEBUG
                              for (i = 0; i <= lgh; i++)
                              printf("id[%d] = '%c' next[%d] = %d\n",
                              i, id[i], i, next[i]);
                              #endif
                              } /* initnext */

                              /* --------------------- */

                              /* reads f without rewinding until either EOF or *marker
                              has been found. Returns EOF if not found. At exit the
                              last matching char has been read, and no further. */
                              int kmpffind(const char *marker, int lgh, int *next, FILE *f)
                              {
                              int j; /* char position in marker to check */
                              int ch; /* current char */

                              assert(lgh 0);
                              j = 0;
                              while ((j < lgh) && (EOF != (ch = getc(f)))) {
                              chrcount++;
                              while ((j >= 0) && (ch != marker[j])) j = next[j];
                              j++;
                              }
                              return ch;
                              } /* kmpffind */

                              /* --------------------- */

                              /* Find marker in f, display following printing chars
                              up to some non printing character or EOF */
                              int binfsrch(const char *marker, FILE *f)
                              {
                              int *next;
                              int lgh;
                              int ch;
                              int items; /* count of markers found */

                              lgh = strlen(marker);
                              if (!(next = malloc(1 + lgh * sizeof *next))) {
                              puts("No memory");
                              exit(EXIT_FAILU RE);
                              }
                              else {
                              initnext(next, marker, lgh);
                              items = 0;
                              while (EOF != kmpffind(marker , lgh, next, f)) {
                              /* found, take appropriate action */
                              items++;
                              printf("%d %s : \"", items, marker);
                              while (isprint(ch = getc(f))) {
                              chrcount++;
                              putchar(ch);
                              }
                              puts("\"");
                              if (EOF == ch) break;
                              else chrcount++;
                              }
                              free(next);
                              return items;
                              }
                              } /* binfsrch */

                              /* --------------------- */

                              int main(int argc, char **argv)
                              {
                              FILE *f;

                              f = stdin;
                              if (3 == argc) {
                              if (!(f = fopen(argv[2], "rb"))) {
                              printf("Can't open %s\n", argv[2]);
                              exit(EXIT_FAILU RE);
                              }
                              argc--;
                              }
                              if (2 != argc) {
                              puts("Usage: binfsrch name [binaryfile]");
                              puts(" (file defaults to stdin text mode)");
                              }
                              else if (binfsrch(argv[1], f)) {
                              printf("\"%s\" : found\n", argv[1]);
                              }
                              else printf("\"%s\" : not found\n", argv[1]);
                              printf("%lu chars\n", (unsigned long)chrcount);
                              return 0;
                              } /* main binfsrch */

                              --
                              [mail]: Chuck F (cbfalconer at maineline dot net)
                              [page]: <http://cbfalconer.home .att.net>
                              Try the download section.


                              ** Posted from http://www.teranews.com **

                              Comment

                              Working...