且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果字节的缓冲区进行签名或无符号的字符缓冲区?

更新时间:2023-11-08 08:07:16


  

应的字节的缓冲器签署
  char或unsigned char或只是一个char
  缓冲? C和之间的差异
  C ++?


块引用>

在语言如何处理这微小的差别。 A 在约​​定如何处理它的区别。


  • 字符 = ASCII(或UTF-8,但带符号获取的方式出现)的文本数据

  • unsigned char型 =字节

  • 符号字符 =很少使用

和有code,它的依赖于这样的区别的。就在两个星期前,我遇到过的JPEG数据越来越损坏,因为它被传递到的char * 版本我们的Base64 EN code函数&MDASH的一个bug;其中有益全部换成了无效的UTF-8,在串。更改为字节又名 unsigned char型是所有花来解决它。

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

Thanks.

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 (or UTF-8, but the signedness gets in the way there) textual data
  • unsigned char = byte
  • signed char = rarely used

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.