Compare char[2] with short

Discussion in 'C Programming' started by spasmous, May 17, 2008.

  1. spasmous

    spasmous Guest

    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?
     
    spasmous, May 17, 2008
    #1
    1. Advertising

  2. spasmous

    Ian Collins Guest

    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.
     
    Ian Collins, May 17, 2008
    #2
    1. Advertising

  3. spasmous

    spasmous Guest

    On May 17, 1:13 pm, Ian Collins <> wrote:
    > 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.
     
    spasmous, May 17, 2008
    #3
  4. spasmous

    Ian Collins Guest

    spasmous wrote:
    > On May 17, 1:13 pm, Ian Collins <> wrote:
    >> 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.


    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.
     
    Ian Collins, May 17, 2008
    #4
  5. spasmous

    Lew Pitcher Guest

    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. ------
     
    Lew Pitcher, May 17, 2008
    #5
  6. 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...
     
    Antoninus Twink, May 17, 2008
    #6
  7. In article <>,
    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?


    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_ARRAY;
    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
     
    Walter Roberson, May 17, 2008
    #7
  8. spasmous <> writes:

    > 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.
     
    Ben Bacarisse, May 17, 2008
    #8
  9. Antoninus Twink <> writes:
    > 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_Keith) <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"
     
    Keith Thompson, May 17, 2008
    #9
  10. spasmous <> writes:
    > 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_Keith) <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"
     
    Keith Thompson, May 17, 2008
    #10
  11. spasmous

    Ike Naar Guest

    In article <>,
    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?


    Perhaps you could try something like:

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

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

    Ike
     
    Ike Naar, May 18, 2008
    #11
  12. Keith Thompson <> writes:
    > spasmous <> writes:
    >> 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_Keith) <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"
     
    Keith Thompson, May 18, 2008
    #12
  13. On 17 May 2008 at 22:04, Keith Thompson wrote:
    > Antoninus Twink <> writes:
    >> 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.
     
    Antoninus Twink, May 18, 2008
    #13
  14. spasmous

    Bart Guest

    On May 17, 10:07 pm, 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?


    Have you considered that your data may not contain "DA"? In which case
    your code will keep going until it encounters DA elsewhere in your
    memory space, or will crash.

    Also you code is only looking at (most likely) even addresses, it will
    not see DA at an odd address.

    --
    Bartc
     
    Bart, May 18, 2008
    #14
  15. 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.


    You're assuming that short is two bytes. Indeed, you seem
    to be making a lot of unportable assumptions.

    > 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


    There's nothing obvious about it. For starters short
    needn't be restricted to 16-bit, and there may be
    padding bits resulting in trap representations.

    > so how do I get that info in a simple way?


    Are you also assuming ASCII? If so, then...

    while (data[j] != 0x4441) j++;

    ....should do the trick.

    --
    Peter
     
    Peter Nilsson, May 19, 2008
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SinusX

    char + short = char*

    SinusX, May 18, 2004, in forum: C++
    Replies:
    5
    Views:
    427
    Sharad Kala
    May 18, 2004
  2. jamx
    Replies:
    11
    Views:
    854
    Barry Schwarz
    Mar 12, 2006
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,129
    Ian Collins
    May 9, 2006
  4. David Geering

    longs, long longs, short short long ints . . . huh?!

    David Geering, Jan 8, 2007, in forum: C Programming
    Replies:
    15
    Views:
    581
    Keith Thompson
    Jan 11, 2007
  5. Replies:
    4
    Views:
    857
    Kaz Kylheku
    Oct 17, 2006
Loading...

Share This Page