473,796 Members | 2,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Comparing memory with a constant

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
0x0001020304050 6070809 where array[0] == 0x00, array[1] == 0x01, etc.
How do I do that. Obviously I can't use memcmp() with the
0x0001020304050 6070809 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!

Nov 22 '06 #1
25 5720
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."

Nov 22 '06 #2
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
0x0001020304050 6070809 where array[0] == 0x00, array[1] == 0x01, etc.
How do I do that. Obviously I can't use memcmp() with the
0x0001020304050 6070809 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.
Nov 22 '06 #3

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
0x0001020304050 6070809 where array[0] == 0x00, array[1] == 0x01, etc.
How do I do that. Obviously I can't use memcmp() with the
0x0001020304050 6070809 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?

Nov 22 '06 #4
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
0x0001020304 0506070809 where array[0] == 0x00, array[1] == 0x01, etc.
How do I do that. Obviously I can't use memcmp() with the
0x0001020304 0506070809 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.
Nov 22 '06 #5

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
0x00010203040 506070809 where array[0] == 0x00, array[1] == 0x01, etc.
How do I do that. Obviously I can't use memcmp() with the
0x00010203040 506070809 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?

Nov 22 '06 #6
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
>0x00010203 040506070809 where array[0] == 0x00, array[1] == 0x01, etc.
>How do I do that. Obviously I can't use memcmp() with the
>0x00010203 040506070809 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
>converti ng 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.
Nov 22 '06 #7

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
0x000102030 40506070809 where array[0] == 0x00, array[1] == 0x01, etc.
How do I do that. Obviously I can't use memcmp() with the
0x000102030 40506070809 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
convertin g 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 0x0001020304050 6070809 or whatever else. Now
obviously the above code won't work but I think you get my idea?

Nov 22 '06 #8
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,0x 02,0x03,0x04,0x 05,0x06,0x07,0x 08,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 0x0001020304050 6070809 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.
Nov 22 '06 #9

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,0x0 2,0x03,0x04,0x0 5,0x06,0x07,0x0 8,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 0x0001020304050 6070809 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.

Nov 22 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2016
by: Jani Yusef | last post by:
I have a HW problem stated as shown at the top of the solution. The thing is is that I am not 100% sure wtf constant memory means. Well, I think I do but I am confused. Does my solution use contant memory in terms of the length of the list i? If so why not and how could I change it to be so? I am sure the solution is O(n) since the list must only iterated once and the dictionary is O(1), correct? Thanks for the help!! #You are given a...
11
461
by: Peter | last post by:
Hi how can I compare two byte arrays in VB.NET Thank Peter
9
5930
by: mahurshi | last post by:
i have a quick question i am putting a debug flag in my program (i really dont need this feature, but i figured it might be useful when i get into trouble) so i want to check if argv is the letter "d" this is what i have so far if (argv) { write_read_input_file(filename); }
8
2934
by: vikram | last post by:
i have series of questions 1.How a c program is loaded in memory i mean the whats is the structure that the code segment?? data segment?? 2.When you say const int *p; where is p stored in the memory?? what happens internal so that its a read only. 3. when declared volatile int *p where exactly in the memory it is stored.
2
1679
by: gavino | last post by:
REHDAT LINUX 4S PHP 4.3.9 LEGACY APP I MOVED NOW EATS MEMORY CAN ANYONE TAE A LOOK ? I FIXED ONE PARTIALLY BY CHANGING TO here are my apache settings: 1 page laoding for a few seconds eat like 48% of a xeon 3.0 processor!!! <IfModule prefork.c> StartServers 8 MinSpareServers 5 MaxSpareServers 20
10
11329
by: william | last post by:
#include <stdio.h> int main() { char *str=NULL; char x="today is good!"; printf("%s", str); str=strtok(x," "); if (str=="today") //<==here is line that confuses me printf("they equals!\n");
25
13062
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void *p, void *q) that will take any two pointers and decide whether they can be compared or not. I really can't think how to do this - any suggestions?
0
9525
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10452
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10221
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10169
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.