Compare char[2] with short

S

spasmous

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?
 
I

Ian Collins

spasmous said:
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!
 
S

spasmous

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.
 
I

Ian Collins

spasmous said:
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;
 
L

Lew Pitcher

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. ------
 
A

Antoninus Twink

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...
 
W

Walter Roberson

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'.
 
B

Ben Bacarisse

spasmous said:
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 */
 
K

Keith Thompson

Antoninus Twink said:
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.
 
K

Keith Thompson

spasmous said:
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().
 
I

Ike Naar

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
 
K

Keith Thompson

Keith Thompson said:
spasmous said:
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.
 
A

Antoninus Twink

Antoninus Twink said:
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.
 
B

Bart

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.
 
P

Peter Nilsson

spasmous said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top