unsigned char和char指针之间的区别 [英] Difference between unsigned char and char pointers
问题描述
我对 unsigned char
(在WinAPI中也是 BYTE
)和 char
指针。
I'm a bit confused with differences between unsigned char
(which is also BYTE
in WinAPI) and char
pointers.
目前我正在使用一些基于ATL的旧代码,的以下表达式:
Currently I'm working with some ATL-based legacy code and I see a lot of expressions like the following:
CAtlArray<BYTE> rawContent;
CALL_THE_FUNCTION_WHICH_FILLS_RAW_CONTENT(rawContent);
return ArrayToUnicodeString(rawContent);
// or return ArrayToAnsiString(rawContent);
现在, ArrayToXXString
的实现
CStringA ArrayToAnsiString(const CAtlArray<BYTE>& array)
{
CAtlArray<BYTE> copiedArray;
copiedArray.Copy(array);
copiedArray.Add('\0');
// Casting from BYTE* -> LPCSTR (const char*).
return CStringA((LPCSTR)copiedArray.GetData());
}
CStringW ArrayToUnicodeString(const CAtlArray<BYTE>& array)
{
CAtlArray<BYTE> copiedArray;
copiedArray.Copy(array);
copiedArray.Add('\0');
copiedArray.Add('\0');
// Same here.
return CStringW((LPCWSTR)copiedArray.GetData());
}
< c $ c>到
LPCSTR
( const char *
)可以安全吗?在将数组数据转换为宽字符串时,是否真的需要添加 double null终止?
Is it really necessary to add double null-termination when converting array data to wide-character string?
转换例程 CStringW((LPCWSTR)copiedArray.GetData())似乎对我无效,
The conversion routine CStringW((LPCWSTR)copiedArray.GetData())
seems invalid to me, is that true?
推荐答案
C标准在定义一个字节时是很奇怪的。你确实有几个保证。
The C standard is kind of weird when it comes to the definition of a byte. You do have a couple of guarantees though.
- 字节总是大小为一个字符
- 返回1
这个定义不适用于老的平台,其中一个字节是6或7位长,但它的意思是
BYTE *,
和char *
保证是等价的。This definition doesn't mesh well with older platforms where a byte was 6 or 7 bits long, but it does mean
BYTE*,
andchar *
are guaranteed to be equivalent.在Unicode字符串的末尾需要多个空值,以零(空)字节开头的Unicode字符。
Multiple nulls are needed at the end of a Unicode string because there are valid Unicode characters that start with a zero (null) byte.
为了使代码更容易阅读,这完全是一个风格问题。这段代码看起来是用很多旧的C Windows代码使用的样式写的,这已经失去了赞成。可能有很多方法让你更清楚,但如何使它更清晰没有明确的答案。
As for making the code easier to read, that is completely a matter of style. This code appears to be written in a style used by a lot of old C Windows code, which has definitely fallen out of favor. There are probably a ton of ways to make it clearer for you, but how to make it clearer has no clear answer.
这篇关于unsigned char和char指针之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!