具有负值的字符 [英] A character with a negative value

查看:78
本文介绍了具有负值的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



普通字符可以是签名或未签名。典型范围可以是:


CHAR_MIN == -128,CHAR_MAX == 127


CHAR_MIN == 0,CHAR_MAX == 255


标准说如果我们将

参数传递给to *,则行为是不确定的。函数的值超出范围0

到UCHAR_MAX。这肯定应该是CHAR_MIN

到CHAR_MAX。


如果有一个特定的实现,其中一个有效的角色有

a负面价值,你是否可以将这个值传递给to *?我认为如果你没有b $ b那将是非常愚蠢的。


例如,让我们说有一个大写按字母顺序

字符,其数值为17,小写形式

此字符的值为-8。如果我们将前者传递给tolower,我们

应该得到-8,如果我们将后者传递给toupper,我们应该得到

17。当然,标准本身并不能保证这一点......但是如果实现对有效字符有负值那么

那么

如果你是非常愚蠢的无法对这些

有效字符进行正常操作。这里有多少人使用unsigned char。当使用to *时,施放

功能?因为我不是。


马丁

解决方案

Martin Wells写道:


作为一个例子,让我们说有一个大写的字母

字符,其数值为17,并且小写形式

这个字符的值是-8。



在一致的实现中不可能发生。


这里有多少人使用 unsigned char使用to *时强制转换



Me。


函数?因为我不是。



糟糕。


-

Chrisunsinged and unsung Dollin


Hewlett-Packard Limited Cain Road,Bracknell,注册号:

注册办事处:Berks RG12 1HN 690597 England


11月1日下午3:18,Martin Wells< war ... @ eircom.netwrote:


Plain char可以是签名或未签名。典型范围可以是:


CHAR_MIN == -128,CHAR_MAX == 127


CHAR_MIN == 0,CHAR_MAX == 255



CHAR_M(IN / AX)用于普通字符。使用(S / U)CHAR_M(IN / MAX)

SCHAR_MIN< = -127 SCHAR_MAX> = 127

UCHAR_MIN 0 UCHAR_MAX> = 255


将所有to *()调用中的参数转换为(unsigned char)。


Chris:


例如,假设有一个大写的字母

字符,其数值为17,并且小写形式

这个角色的值是-8。



在符合要求的实施中不可能发生。



特别是在符合要求的实施中不会发生什么?


马丁



Plain char may be signed or unsigned. Typical ranges could be:

CHAR_MIN == -128, CHAR_MAX == 127

CHAR_MIN == 0, CHAR_MAX == 255

The Standard says that the behaviour is undefined if we pass an
argument to the "to*" functions whose value is outside the range of 0
through UCHAR_MAX. This most certainly should have been CHAR_MIN
through CHAR_MAX.

If there were a particular implementation where a valid character had
a negative value, wouldn''t it make perfect sense that you can pass
this value to "to*"? I think it would be ridiculously stupid if you
couldn''t.

As an example, let''s say that there''s an uppercase alphabetical
character whose numeric value is 17, and that the lowercase form of
this character''s value is -8. If we pass the former to "tolower", we
should get -8, and if we pass the latter to "toupper", we should get
17. Now of course, the Standard itself doesn''t guarantee this... but
if the implementation has negative values for valid characters then it
would be quite stupid if you couldn''t do normal operations on these
valid characters. How many people here use an "unsigned char" cast
when using the "to*" functions? Because I don''t.

Martin

解决方案

Martin Wells wrote:

As an example, let''s say that there''s an uppercase alphabetical
character whose numeric value is 17, and that the lowercase form of
this character''s value is -8.

Can''t happen in a conforming implementation.

How many people here use an "unsigned char" cast when using the "to*"

Me.

functions? Because I don''t.

Oops.

--
Chris "unsinged and unsung" Dollin

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England


On Nov 1, 3:18 pm, Martin Wells <war...@eircom.netwrote:

Plain char may be signed or unsigned. Typical ranges could be:

CHAR_MIN == -128, CHAR_MAX == 127

CHAR_MIN == 0, CHAR_MAX == 255

CHAR_M(IN/AX) is for plain char. use (S/U)CHAR_M(IN/MAX)
SCHAR_MIN <= -127 SCHAR_MAX >= 127
UCHAR_MIN 0 UCHAR_MAX >= 255

Cast the argument in all your to*() calls to (unsigned char).


Chris:

As an example, let''s say that there''s an uppercase alphabetical
character whose numeric value is 17, and that the lowercase form of
this character''s value is -8.


Can''t happen in a conforming implementation.


Specifically what can''t happen in a conforming implementation?

Martin


这篇关于具有负值的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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