比较内存与常量 [英] Comparing memory with a constant

查看:95
本文介绍了比较内存与常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想用一个常数来比较一组无符号字符(基本上只是数据

,没有任何上下文),我是不知道怎么做。

假设我的数组是数组[10],我想将它与常量比较

0x00010203040506070809其中array [0] == 0x00, array [1] == 0x01等

如何做到这一点。显然我不能将memcmp()与

0x00010203040506070809一起使用,因为编译器会将该常量用作

地址并给我一个段错误。除了对i的所有10个值执行if(array [i] =

0x0i)之外,我该如何进行这样的比较?我想到了

将常量转换为字符串并使用

执行memcmp / strcmp该字符串但不是所有字节都是可打印的ASCII字符。


谢谢!

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!

推荐答案

galapogos写道:
galapogos wrote:

我该怎么做。显然我不能使用memcmp()
How do I do that. Obviously I can''t use memcmp()



这很明显,但我认为不是你想的原因。
例如,考虑字节顺序和对齐。


根据长码的生成方式,你可能无法用b $ b来做你所做的事情。完全建议,任何可移植性。


您正在假设一个

数组的按位顺序和对齐方式,以及关于按位表示的很长的价值。

这些属于你可能会幸运的类别。

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."


galapogos写道:
galapogos wrote:



我正在尝试比较一组无符号字符(基本上只是数据

,没有任何上下文)有一个常数,我不知道该怎么做。

我的数组是数组[10],我想把它与常数比较

0x00010203040506070809其中array [0] == 0x00,array [1] == 0x01等。

我该怎么做。显然我不能将memcmp()与

0x00010203040506070809一起使用,因为编译器会将该常量用作

地址并给我一个段错误。除了对i的所有10个值执行if(array [i] =

0x0i)之外,我该如何进行这样的比较?我想到了

将常量转换为字符串并使用

执行memcmp / strcmp该字符串但不是所有字节都是可打印的ASCII字符。
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.



只需将常量转换为unsigned char数组并使用memcmp。


如何表示十字节常量?


-

Ian Collins。

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

How do you represent a ten byte constant?

--
Ian Collins.




Ian Collins写道:

Ian Collins wrote:

galapogos写道:
galapogos wrote:



我想比较一个无符号字符数组(基本上只是数据

没有任何上下文)和常量,我不知道该怎么做。

我的数组是数组[ 10]我希望将它与常数比较

0x00010203040506070809其中array [0] == 0x00,array [1] == 0x01等。

我如何去做。显然我不能将memcmp()与

0x00010203040506070809一起使用,因为编译器会将该常量用作

地址并给我一个段错误。除了对i的所有10个值执行if(array [i] =

0x0i)之外,我该如何进行这样的比较?我想到了

将常量转换为字符串并使用

执行memcmp / strcmp该字符串但不是所有字节都是可打印的ASCII字符。
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.



只需将常量转换为unsigned char数组并使用memcmp。


如何表示十字节常量?


-

伊恩柯林斯。

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?


这篇关于比较内存与常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆