字节数组的Char和unsigned char [英] Char vs unsigned char for byte arrays

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

问题描述

当存储字节数组(blob ...)时最好使用 char unsigned char 对于项目( unsigned char aka uint8_t )? (标准说, sizeof 两者都是1个字节。)

When storing "byte arrays" (blobs...) is it better to use char or unsigned char for the items (unsigned char a.k.a. uint8_t)? (Standard says that sizeof of both is precisely 1 Byte.)

还是一个比另一个更方便或流行?也许,Boost使用的库是什么?

Does it matter at all? Or one is more convenient or prevalent than the other? Maybe, what libraries like Boost do use?

推荐答案

如果 char 当升高到 int 时,对高位设置的字节值执行算术将导致符号扩展;因此,例如:

If char is signed, then performing arithmetic on a byte value with the high bit set will result in sign extension when promoting to int; so, for example:

char c = '\xf0';
int res = (c << 24) | (c << 16) | (c << 8) | c;

将会提供 0xfffffff0 $ c> 0xf0f0f0f0 。这可以通过使用 0xff 屏蔽来避免。

will give 0xfffffff0 instead of 0xf0f0f0f0. This can be avoided by masking with 0xff.

char

注意,如果你使用它代替 unsigned char

Note that a cast from char * to/from unsigned char * is always safe (3.9p2). A philosophical reason to favour unsigned char is that 3.9p4 in the standard favours it, at least for representing byte arrays that could hold memory representations of objects:


T 类型的对象的对象表示 code> T 对象占用的对象占用的code> N unsigned char ,其中 N 等于 sizeof(T)

The object representation of an object of type T is the sequence of N unsigned char objects taken up by the object of type T, where N equals sizeof(T).

这篇关于字节数组的Char和unsigned char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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