Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Compare char[2] with short

Reply
Thread Tools

Compare char[2] with short

 
 
spasmous
Guest
Posts: n/a
 
      05-17-2008
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?
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      05-17-2008
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.
 
Reply With Quote
 
 
 
 
spasmous
Guest
Posts: n/a
 
      05-17-2008
On May 17, 1:13*pm, Ian Collins <(E-Mail Removed)> 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.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-17-2008
spasmous wrote:
> On May 17, 1:13 pm, Ian Collins <(E-Mail Removed)> 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.
 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      05-17-2008
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. ------


 
Reply With Quote
 
Antoninus Twink
Guest
Posts: n/a
 
      05-17-2008
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' << , but that relies on your machine
being little endian...

 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      05-17-2008
In article <(E-Mail Removed)>,
spasmous <(E-Mail Removed)> 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
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      05-17-2008
spasmous <(E-Mail Removed)> 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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-17-2008
Antoninus Twink <(E-Mail Removed)> 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' << , 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.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-17-2008
spasmous <(E-Mail Removed)> 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) (E-Mail Removed) <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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference of extern short *x and extern short x[]? Andre C Programming 5 07-17-2012 07:38 PM
unsigned short, short literals Ioannis Vranos C Programming 5 03-05-2008 01:25 AM
longs, long longs, short short long ints . . . huh?! David Geering C Programming 15 01-11-2007 09:39 PM
unsigned short short? slougheed@gmail.com C++ 4 10-16-2006 11:25 PM
Wireless distance far too short M Skabialka Wireless Networking 3 01-14-2005 07:51 PM



Advertisments