Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Comparing memory with a constant

Reply
Thread Tools

Comparing memory with a constant

 
 
galapogos
Guest
Posts: n/a
 
      11-22-2006
Hi,
I'm trying to compare an array of unsigned chars(basically just data
without any context) with a constant, and I'm not sure how to do that.
Say my array is array[10] and I want to compare it with the constant
0x00010203040506070809 where array[0] == 0x00, array[1] == 0x01, etc.
How do I do that. Obviously I can't use memcmp() with the
0x00010203040506070809 since the compiler will use that constant as an
address and give me a segfault. Other than doing an if (array[i] =
0x0i) for all 10 values of i, how can I do such a compare? I thought of
converting the constant to a string and doing a memcmp/strcmp using
that string but not all the bytes are printable ASCII characters.

Thanks!

 
Reply With Quote
 
 
 
 
james of tucson
Guest
Posts: n/a
 
      11-22-2006
galapogos wrote:

> How do I do that. Obviously I can't use memcmp()


That's obvious but I think not for the reason you are thinking of.
Consider byte-order and alignment for instance.

Depending on how your long number was generated, you might not be able
to do what you're suggesting at all, with any portability.

You are making assumptions about the bitwise order and alignment of an
array, and also about the bitwise representation of a long value. Both
of these fall under the category of "you might get lucky."

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      11-22-2006
galapogos wrote:
> Hi,
> I'm trying to compare an array of unsigned chars(basically just data
> without any context) with a constant, and I'm not sure how to do that.
> Say my array is array[10] and I want to compare it with the constant
> 0x00010203040506070809 where array[0] == 0x00, array[1] == 0x01, etc.
> How do I do that. Obviously I can't use memcmp() with the
> 0x00010203040506070809 since the compiler will use that constant as an
> address and give me a segfault. Other than doing an if (array[i] =
> 0x0i) for all 10 values of i, how can I do such a compare? I thought of
> converting the constant to a string and doing a memcmp/strcmp using
> that string but not all the bytes are printable ASCII characters.
>

Just convert your constant to an array of unsigned char and use memcmp.

How do you represent a ten byte constant?

--
Ian Collins.
 
Reply With Quote
 
galapogos
Guest
Posts: n/a
 
      11-22-2006

Ian Collins wrote:
> galapogos wrote:
> > Hi,
> > I'm trying to compare an array of unsigned chars(basically just data
> > without any context) with a constant, and I'm not sure how to do that.
> > Say my array is array[10] and I want to compare it with the constant
> > 0x00010203040506070809 where array[0] == 0x00, array[1] == 0x01, etc.
> > How do I do that. Obviously I can't use memcmp() with the
> > 0x00010203040506070809 since the compiler will use that constant as an
> > address and give me a segfault. Other than doing an if (array[i] =
> > 0x0i) for all 10 values of i, how can I do such a compare? I thought of
> > converting the constant to a string and doing a memcmp/strcmp using
> > that string but not all the bytes are printable ASCII characters.
> >

> Just convert your constant to an array of unsigned char and use memcmp.
>
> How do you represent a ten byte constant?
>
> --
> Ian Collins.


That was what I was hoping to avoid. If I have to convert the constant
to an array, I might as well save some space and compare each byte of
the array with each byte of the constant. I guess there's no easier way?

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      11-22-2006
galapogos wrote:
> Ian Collins wrote:
>
>>galapogos wrote:
>>
>>>Hi,
>>>I'm trying to compare an array of unsigned chars(basically just data
>>>without any context) with a constant, and I'm not sure how to do that.
>>>Say my array is array[10] and I want to compare it with the constant
>>>0x00010203040506070809 where array[0] == 0x00, array[1] == 0x01, etc.
>>>How do I do that. Obviously I can't use memcmp() with the
>>>0x00010203040506070809 since the compiler will use that constant as an
>>>address and give me a segfault. Other than doing an if (array[i] =
>>>0x0i) for all 10 values of i, how can I do such a compare? I thought of
>>>converting the constant to a string and doing a memcmp/strcmp using
>>>that string but not all the bytes are printable ASCII characters.
>>>

>>
>>Just convert your constant to an array of unsigned char and use memcmp.
>>
>>How do you represent a ten byte constant?
>>

> That was what I was hoping to avoid. If I have to convert the constant
> to an array, I might as well save some space and compare each byte of
> the array with each byte of the constant. I guess there's no easier way?
>

How is the constant represented?

--
Ian Collins.
 
Reply With Quote
 
galapogos
Guest
Posts: n/a
 
      11-22-2006

Ian Collins wrote:
> galapogos wrote:
> > Ian Collins wrote:
> >
> >>galapogos wrote:
> >>
> >>>Hi,
> >>>I'm trying to compare an array of unsigned chars(basically just data
> >>>without any context) with a constant, and I'm not sure how to do that.
> >>>Say my array is array[10] and I want to compare it with the constant
> >>>0x00010203040506070809 where array[0] == 0x00, array[1] == 0x01, etc.
> >>>How do I do that. Obviously I can't use memcmp() with the
> >>>0x00010203040506070809 since the compiler will use that constant as an
> >>>address and give me a segfault. Other than doing an if (array[i] =
> >>>0x0i) for all 10 values of i, how can I do such a compare? I thought of
> >>>converting the constant to a string and doing a memcmp/strcmp using
> >>>that string but not all the bytes are printable ASCII characters.
> >>>
> >>
> >>Just convert your constant to an array of unsigned char and use memcmp.
> >>
> >>How do you represent a ten byte constant?
> >>

> > That was what I was hoping to avoid. If I have to convert the constant
> > to an array, I might as well save some space and compare each byte of
> > the array with each byte of the constant. I guess there's no easier way?
> >

> How is the constant represented?
>
> --
> Ian Collins.


That's a problem isn't it? A 10 byte constant can't be represented in
any data format, so I guess I have to split it up?

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      11-22-2006
galapogos wrote:
> Ian Collins wrote:
>
>>galapogos wrote:
>>
>>>Ian Collins wrote:
>>>
>>>
>>>>galapogos wrote:
>>>>
>>>>
>>>>>Hi,
>>>>>I'm trying to compare an array of unsigned chars(basically just data
>>>>>without any context) with a constant, and I'm not sure how to do that.
>>>>>Say my array is array[10] and I want to compare it with the constant
>>>>>0x00010203040506070809 where array[0] == 0x00, array[1] == 0x01, etc.
>>>>>How do I do that. Obviously I can't use memcmp() with the
>>>>>0x00010203040506070809 since the compiler will use that constant as an
>>>>>address and give me a segfault. Other than doing an if (array[i] =
>>>>>0x0i) for all 10 values of i, how can I do such a compare? I thought of
>>>>>converting the constant to a string and doing a memcmp/strcmp using
>>>>>that string but not all the bytes are printable ASCII characters.
>>>>>
>>>>
>>>>Just convert your constant to an array of unsigned char and use memcmp.
>>>>
>>>>How do you represent a ten byte constant?
>>>>
>>>
>>>That was what I was hoping to avoid. If I have to convert the constant
>>>to an array, I might as well save some space and compare each byte of
>>>the array with each byte of the constant. I guess there's no easier way?
>>>

>>
>>How is the constant represented?
>>

>
> That's a problem isn't it? A 10 byte constant can't be represented in
> any data format, so I guess I have to split it up?
>

Please trim signatures in replies.

const unsigned char ref[] =
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09 };


--
Ian Collins.
 
Reply With Quote
 
galapogos
Guest
Posts: n/a
 
      11-22-2006

Ian Collins wrote:
> galapogos wrote:
> > Ian Collins wrote:
> >
> >>galapogos wrote:
> >>
> >>>Ian Collins wrote:
> >>>
> >>>
> >>>>galapogos wrote:
> >>>>
> >>>>
> >>>>>Hi,
> >>>>>I'm trying to compare an array of unsigned chars(basically just data
> >>>>>without any context) with a constant, and I'm not sure how to do that.
> >>>>>Say my array is array[10] and I want to compare it with the constant
> >>>>>0x00010203040506070809 where array[0] == 0x00, array[1] == 0x01, etc.
> >>>>>How do I do that. Obviously I can't use memcmp() with the
> >>>>>0x00010203040506070809 since the compiler will use that constant as an
> >>>>>address and give me a segfault. Other than doing an if (array[i] =
> >>>>>0x0i) for all 10 values of i, how can I do such a compare? I thought of
> >>>>>converting the constant to a string and doing a memcmp/strcmp using
> >>>>>that string but not all the bytes are printable ASCII characters.
> >>>>>
> >>>>
> >>>>Just convert your constant to an array of unsigned char and use memcmp.
> >>>>
> >>>>How do you represent a ten byte constant?
> >>>>
> >>>
> >>>That was what I was hoping to avoid. If I have to convert the constant
> >>>to an array, I might as well save some space and compare each byte of
> >>>the array with each byte of the constant. I guess there's no easier way?
> >>>
> >>
> >>How is the constant represented?
> >>

> >
> > That's a problem isn't it? A 10 byte constant can't be represented in
> > any data format, so I guess I have to split it up?
> >

> Please trim signatures in replies.
>
> const unsigned char ref[] =
> {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09 };


Thanks. I thought of doing that, but I'm not really comparing to just 1
constant, so having multiple constants would be kinda unwieldy. What
I'm doing is something like that

switch (array) {
case bitpattern1: ...
case bitpattern2: ...
etc...
}

where bitpattern is my 0x00010203040506070809 or whatever else. Now
obviously the above code won't work but I think you get my idea?

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      11-22-2006
galapogos wrote:
> Ian Collins wrote:
>
>>galapogos wrote:
>>
>>>Ian Collins wrote:
>>>
>>>
>>>>galapogos wrote:
>>>>
>>>>>That was what I was hoping to avoid. If I have to convert the constant
>>>>>to an array, I might as well save some space and compare each byte of
>>>>>the array with each byte of the constant. I guess there's no easier way?
>>>>>
>>>>
>>>>How is the constant represented?
>>>>
>>>
>>>That's a problem isn't it? A 10 byte constant can't be represented in
>>>any data format, so I guess I have to split it up?
>>>

>>
>>Please trim signatures in replies.
>>
>> const unsigned char ref[] =
>>{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x 09};

>
>
> Thanks. I thought of doing that, but I'm not really comparing to just 1
> constant, so having multiple constants would be kinda unwieldy. What
> I'm doing is something like that
>
> switch (array) {
> case bitpattern1: ...
> case bitpattern2: ...
> etc...
> }
>
> where bitpattern is my 0x00010203040506070809 or whatever else. Now
> obviously the above code won't work but I think you get my idea?
>

But you still have to represent the constants, don't you?

You'll have to fall back on a set of if/else tests with memcmp and const
unsigned char arrays.

--
Ian Collins.
 
Reply With Quote
 
galapogos
Guest
Posts: n/a
 
      11-22-2006

Ian Collins wrote:
> galapogos wrote:
> > Ian Collins wrote:
> >
> >>galapogos wrote:
> >>
> >>>Ian Collins wrote:
> >>>
> >>>
> >>>>galapogos wrote:
> >>>>
> >>>>>That was what I was hoping to avoid. If I have to convert the constant
> >>>>>to an array, I might as well save some space and compare each byte of
> >>>>>the array with each byte of the constant. I guess there's no easier way?
> >>>>>
> >>>>
> >>>>How is the constant represented?
> >>>>
> >>>
> >>>That's a problem isn't it? A 10 byte constant can't be represented in
> >>>any data format, so I guess I have to split it up?
> >>>
> >>
> >>Please trim signatures in replies.
> >>
> >> const unsigned char ref[] =
> >>{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x 09};

> >
> >
> > Thanks. I thought of doing that, but I'm not really comparing to just 1
> > constant, so having multiple constants would be kinda unwieldy. What
> > I'm doing is something like that
> >
> > switch (array) {
> > case bitpattern1: ...
> > case bitpattern2: ...
> > etc...
> > }
> >
> > where bitpattern is my 0x00010203040506070809 or whatever else. Now
> > obviously the above code won't work but I think you get my idea?
> >

> But you still have to represent the constants, don't you?
>
> You'll have to fall back on a set of if/else tests with memcmp and const
> unsigned char arrays.
>

Yes I know that's the worst case scenario that would work. I was just
hoping there was an easier way that I can implement the switch/case
statements rather than using a bunch of if-else statements that I'm
currently doing.

 
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
"error C2057: expected constant expression", "error C2466: cannot allocate an array of constant size 0". Why doesn't my simple program work??? hn.ft.pris@gmail.com C++ 13 01-22-2007 02:03 PM
pointers to constant characters and constant pointers to characters sam_cit@yahoo.co.in C Programming 4 12-14-2006 11:10 PM
len(var) is [CONSTANT] equal to len(var) == [CONSTANT]? Tor Erik Soenvisen Python 14 11-23-2006 09:57 PM
"Non-constant" constant can't be used as template argument Martin Magnusson C++ 2 10-08-2004 08:41 AM
Understanding How To Use #ifdef Constant #define Constant Sequence In Multible Files Christopher M. Lusardi C++ 1 09-02-2004 07:43 AM



Advertisments