字节缓冲区应该是有符号还是无符号字符缓冲区?

2022-01-12 00:00:00 c char buffer c++

字节缓冲区应该是有符号字符还是无符号字符,或者只是一个字符缓冲区?C 和 C++ 有什么区别?

Should a buffer of bytes be signed char or unsigned char or simply a char buffer? Any differences between C and C++?

谢谢.

推荐答案

是否应该对字节缓冲区进行签名char 或 unsigned char 或只是一个 char缓冲?C和C之间的任何差异C++?

Should a buffer of bytes be signed char or unsigned char or simply a char buffer? Any differences between C and C++?

语言处理它的方式略有不同.约定的处理方式存在巨大的差异.

A minor difference in how the language treats it. A huge difference in how convention treats it.

  • char = ASCII(或 UTF-8,但有符号阻碍)textual data
  • 无符号字符 = 字节
  • signed char = 很少使用
  • char = ASCII (or UTF-8, but the signedness gets in the way there) textual data
  • unsigned char = byte
  • signed char = rarely used

并且有代码依赖这种区别.就在一两个星期前,我遇到了一个错误,即 JPEG 数据被损坏,因为它被传递给我们的 Base64 编码函数的 char* 版本......其中有用"替换了字符串"中所有无效的 UTF-8.只需更改为 BYTE aka unsigned char 即可修复它.

And there is code that relies on such a distinction. Just a week or two ago I encountered a bug where JPEG data was getting corrupted because it was being passed to the char* version of our Base64 encode function — which "helpfully" replaced all the invalid UTF-8 in the "string". Changing to BYTE aka unsigned char was all it took to fix it.

相关文章