问题描述
在什么情况下我们需要签名的字符?我想这的唯一用途是将char数量转换为整数。
What are the possible situations where would we need a signed char? I guess the only use of this is in conversion of a char quantity to an integer.
推荐答案
如果我没记错的话, char可能是带符号的也可以是无符号的(取决于编译器/实现)。如果您需要一个未签名的字符,则应明确要求它(带有未签名的字符),如果需要一个已签名的字符,则应明确要求它(带有已签名的字符)。
If I remember right, a "char" may be signed or unsigned (it depends on the compiler/implementation). If you need an unsigned char you should explicitly ask for it (with "unsigned char") and if you need a signed char you should explicitly ask for it (with "signed char").
char只是一个(通常为8位)整数。
A "char" is just a (typically 8-bit) integer. It has nothing to do with characters.
一个字符可以是任何东西,具体取决于您在做什么。我更喜欢使用 uint32_t和Unicode(UTF-32)。对于使用ASCII的老旧软件,可以使用char(不管 char是带符号的还是无符号的)。对于UTF-8,您可能希望使用 unsigned char或 uint8_t。
A character could be anything, depending on what you're doing. I prefer using "uint32_t" and Unicode (UTF-32). For crusty old/broken software that uses ASCII, a char is fine (regardless of whether "char" is signed or unsigned). For UTF-8 you'd probably want to use "unsigned char" or "uint8_t".
您可能还会尝试使用 wchar_t(以及 wchar.h标头),但有很多方法可能会出错(如果您很想尝试,请做一些研究)。
You might also be tempted to try to use "wchar_t" (and the "wchar.h" header), but there's lots of ways that can go wrong (do some research if you're tempted).
这篇关于字符是签名还是未签名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!