On 2009-11-24, petertwocakes <> wrote:
> I'm trying to test a sequence of 4 characters from a ptr buffer
> against a long, but the test fails even though I think they should
> have the same value. e.g :
Because the meaning of a multiple-byte character concept is
implementation defined.
> char word[4] = {'w', 'o', 'r', 'd'};
> char *wordPtr = (char*)wordArray;
> long wordLong = 'word';
> long *longPtr = (long*)wordArrayPtr;
> long longPtrVal = *longPtr;
> Yet, in the end wordLong = 2003792484, but longPtrVal = 1685221239
> Shouldn't they be same?
Not necessarily.
> If not, how do test 4 character sequences in chunks like this?
> (without laboriously testing each char individually)
> I'm in a very large text buffer incrementing the current ptr until I
> hit "word"
First off, learn a bit more about your implementation. On a whole lot
of modern hardware, what you're doing is going to be MUCH more expensive
than testing individual characters, because you're going to be making
unaligned accesses -- which can kill you completely or merely be slow.
If it's running at all, it's probably slow.
Secondly, there is no intrinsic right answer to the question of what order
the bytes in a long are stored. x86 systems typically have the lowest-order
bits first. You might find it more rewarding to look at the bytes in order
of 0x11223344UL. If they're 44, 33, 22, 11, then you would need to take that
into account.
But in practice: strstr(buf, "word") is quite likely to be faster than
whatever you write.
-s
--
Copyright 2009, all wrongs reversed. Peter Seebach /
usenet-
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!