469,920 Members | 2,485 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,920 developers. It's quick & easy.

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?
Jun 27 '08 #1
17 3070
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.
Jun 27 '08 #2
On May 17, 1:13*pm, Ian Collins <ian-n...@hotmail.comwrote:
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.
Jun 27 '08 #3
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. ------
Jun 27 '08 #4
spasmous wrote:
On May 17, 1:13 pm, Ian Collins <ian-n...@hotmail.comwrote:
>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.
Jun 27 '08 #5
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...

Jun 27 '08 #6
In article <15**********************************@t12g2000prg. googlegroups.com>,
spasmous <sp******@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_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
Jun 27 '08 #7
spasmous <sp******@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.
Jun 27 '08 #8
Antoninus Twink <no****@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_Keith) ks***@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"
Jun 27 '08 #9
spasmous <sp******@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_Keith) ks***@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"
Jun 27 '08 #10
On Sat, 17 May 2008 17:24:14 -0400, Lew Pitcher
<lp******@teksavvy.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.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jun 27 '08 #11
In article <15**********************************@t12g2000prg. googlegroups.com>,
spasmous <sp******@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
Jun 27 '08 #12
Keith Thompson <ks***@mib.orgwrites:
spasmous <sp******@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_Keith) ks***@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"
Jun 27 '08 #13
On 17 May 2008 at 22:04, Keith Thompson wrote:
Antoninus Twink <no****@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.

Jun 27 '08 #14
Ike Naar wrote:
spasmous <sp******@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, jo**@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_FAILURE);
}
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_FAILURE);
}
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 **
Jun 27 '08 #15
On May 17, 10:07*pm, spasmous <spasm...@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?
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
Jun 27 '08 #16
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?
Do you really need to use a pointer to short?
Is that a genuine requirement?

--
pete
Jun 27 '08 #17
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
Jun 27 '08 #18

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by Charles Kerekes | last post: by
13 posts views Thread by MrCoder | last post: by
15 posts views Thread by Steffen Loringer | last post: by
6 posts views Thread by yeti | last post: by
2 posts views Thread by sam.barker0 | last post: by
5 posts views Thread by Neel | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.