VS中int8_t字符;这是最好的? [英] int8_t vs char ; Which is the best one?

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

问题描述

我的问题可能会然而迷惑你,我知道这两个是不同类型的(符号字符字符),我公司编码规范,指定要使用中int8_t 而不是字符

My question may be confuse you, I know both are different types (signed char and char), however my company coding guidelines specifies to use int8_t instead of char.

所以,我想知道,为什么我要使用中int8_t 而不是字符类型。是否有任何的最佳做法,以使用中int8_t

So, I want to know, why I have to use int8_t instead of char type. Is there any best practices to use int8_t?

推荐答案

中int8_t 使用的是某些情况下完美的 - 特别是当类型用于计算在哪里一个符号的8位的值是必需的。涉及严格大小的数据计算[例如受外部需求定义为在结果正好是8位(我使用的像素色彩层次上述评论,但真的会 uint8_t有,因为通常为负的像素颜色不要T存在 - 也许除了YUV色彩空间的类型)。

The use of int8_t is perfectly good for some circumstances - specifically when the type is used for calculations where a signed 8-bit value is required. Calculations involving strictly sized data [e.g. defined by external requirements to be exactly 8 bit in the result] (I used pixel colour levels in a comment above, but that really would be uint8_t, as negative pixel colours usually don't exist - except perhaps in YUV type colourspace).

中int8_t 类型不应该被用作字符串替换字符的。这可能会导致编译器错误(或警告,但我们真的不希望有要么处理来自编译器警告)。例如:

The type int8_t should NOT be used as a replacement of char in for strings. This can lead to compiler errors (or warnings, but we don't really want to have to deal with warnings from the compiler either). For example:

int8_t *x = "Hello, World!\n";

printf(x);

可能汇编编译器的罚款,但给的编译器B.或者,如果;混合签署错误或警告和unsigned char值中int8_t 甚至不使用字符类型。这就像期待

may well compile fine on compiler A, but give errors or warnings for mixing signed and unsigned char values on compiler B. Or if int8_t isn't even using a char type. That's just like expecting

int *ptr = "Foo";

在一个现代化的编译器来编译...

to compile in a modern compiler...

在换句话说,中int8_t 就应当,如果您使用的是8位数据caclulation使用字符的。这是不正确的批发与替换所有字符中int8_t ,因为他们远离保证是一样的。

In other words, int8_t SHOULD be used instead of char if you are using 8-bit data for caclulation. It is incorrect to wholesale replace all char with int8_t, as they are far from guaranteed to be the same.

如果有需要使用字符字符串/文字/等,出于某种原因,字符是过于含糊(它可以被符号或无符号等),然后usign 的typedef字符mychar; 或类似的东西应该被使用。 (这可能可以找到一个比一个更好的名字 mychar !)

If there is a need to use char for string/text/etc, and for some reason char is too vague (it can be signed or unsigned, etc), then usign typedef char mychar; or something like that should be used. (It's probably possible to find a better name than mychar!)

编辑:我要指出,你是否同意这种与否,我认为这将是相当愚蠢简单地走到谁负责的这个原则在公司,指向一个职位上的SO和说:我觉得你错了。试着去了解的动机是什么。可能有更多的东西比满足眼睛。

I should point out that whether you agree with this or not, I think it would be rather foolish to simply walk up to whoever is in charge of this "principle" at the company, point at a post on SO and say "I think you're wrong". Try to understand what the motivation is. There may be more to it than meets the eye.

这篇关于VS中int8_t字符;这是最好的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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