为什么要短*而不是char *的字符串? char *和unsigned char *之间的区别? [英] Why short* instead of char* for string? Difference between char* and unsigned char*?

查看:284
本文介绍了为什么要短*而不是char *的字符串? char *和unsigned char *之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如标题所示,我有两个问题。

As the title says, I'm having two questions.

编辑:为了说明, c $ c> char 和 short ,它们通过特定typedef确保它们是8位和16位。然后将实际类型调用 UInt8 UInt16

Edit: To clarify, they don't actually use char and short, they ensure them to be 8-bit and 16-bit by specific typedefs. The actual type is then called UInt8 and UInt16.

1。问题

iTunes SDK使用 unsigned short * 是必需的。使用它而不是 char * / unsigned char * 的优点是什么?如何将其转换为 char * ,而在使用此类型时有什么不同?

The iTunes SDK uses unsigned short* where a string is needed. What are the advantages of using it instead of char*/unsigned char*? How to convert it to char*, and what differs when working with this type instead?

2 。问题

当字符串必须存储时,我才看到 char * 什么时候使用 unsigned char * 那么,还是不会有什么区别?

I've only seen char* when a string must be stored, yet. When should I use unsigned char* then, or doesn't it make any difference?

推荐答案

无符号short 数组可以与宽字符串一起使用 - 例如,如果你有UTF-16编码文本 - 虽然我期望看到 wchar_t 。但是他们可能有他们的原因,如在MacOS和Windows之间兼容。 (如果我的来源是正确的,MacOS的 wchar_t 是32位,而Windows'是16位。)

unsigned short arrays can be used with wide character strings - for instance if you have UTF-16 encoded texts - although I'd expect to see wchar_t in those cases. But they may have their reasons, like being compatible between MacOS and Windows. (If my sources are right, MacOS' wchar_t is 32 bits, while Windows' is 16 bits.)

通过调用相应的库函数在两种类型的字符串之间进行转换。哪个功能是合适的取决于情况。

You convert between the two types of string by calling the appropriate library function. Which function is appropriate depends on the situation. Doesn't the SDK come with one?

char 而不是 unsigned char ,所有字符串历史上总是用 char 定义,因此切换到 unsigned char 将导致不兼容。

(切换到 signed char 也会导致不兼容,但不知何故......)

And char instead of unsigned char, well, all strings have historically always been defined with char, so switching to unsigned char would introduce incompatibilities.
(Switching to signed char would also cause incompatibilities, but somehow not as many...)

编辑现在问题已编辑,让我说我在输入我的答案之前没有看到修改。但是,对于上述原因, UInt16 是一个16位实体比wchar_t更好的表示。

Edit Now the question has been edited, let me say that I didn't see the edits before I typed my answer. But yes, UInt16 is a better representation of a 16 bit entity than wchar_t for the above reason.

这篇关于为什么要短*而不是char *的字符串? char *和unsigned char *之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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