负的ASCII值 [英] Negative ASCII value

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

问题描述

什么是负面的ASCII值的点?

  int类型的='«'; // A = -85,但在ASCII表'<<'应该是174


解决方案

这是你的编译器字符类型为一的签署的整数的假象类型和 INT 是一个更广泛的符号整型,因此,字符常量被认为是一个负数,是符号扩展到更广泛的整数类型。

有没有它多大意义,它只是发生。 C标准允许编译器实现来选择是否考虑字符要签名或签名。一些编译器甚至有编译时切换到更改默认设置。如果你想确保有关字符键入,明确写入符号字符 unsigned char型,分别为。

使用的 unsigned char型扩展到一个 INT 来避免负面 INT 价值,或者打开一个全新的潘多拉盒子,享受 WCHAR

What's the point of negative ASCII values?

int a = '«'; //a = -85 but as in ASCII table '<<' should be 174

解决方案

This is an artefact of your compiler's char type being a signed integer type, and int being a wider signed integer type, and thus the character constant is considered a negative number and is sign-extended to the wider integer type.

There is not much sense in it, it just happens. The C standard allows for compiler implementations to choose whether they consider char to be signed or unsigned. Some compilers even have compile time switches to change the default. If you want to make sure about the signedness of the char type, explicitly write signed char or unsigned char, respectively.

Use an unsigned char to be extended to an int to avoid the negative int value, or open a whole new Pandora's box and enjoy wchar.

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

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