uint8和C中的char之间的转换 [英] Conversion between uint8 and char in C
问题描述
我有一个API,可实现对EEPROM的写操作.这是它的声明:
I have an API that implements a writing operation to EEPROM. Here is its declaration:
CYBLE_API_RESULT_T CyBle_StoreAppData (uint8 * srcBuff, const uint8 destAddr[], uint32 buffLen, uint8 isForceWrite);
当我调用此函数并将数组参数发送到已声明为uint8
类型的srcBuff
时,它运行良好.
It is working well when I call this function and send an array parameter to srcBuff
which has been declared as uint8
type.
问题是,我需要向它发送char
数组指针.我以为char
已经是uint8
,但是如果我向该函数发送char
数组指针而不是uint8
,则会收到编译器警告.为什么我不能使用char
而不是uint8
?这是调用该函数的2个示例:
The problem is, I need to send char
array pointer to it. I was thinking that char
is already a uint8
, but I get a compiler warning if I send a char
array pointer to that function instead of uint8
. Why can't I use char
instead of uint8
? Here are 2 examples of calling that function:
static const uint8 datastack_ROM[dedicatedRomSize] = {0};
uint8 Container_ID[10];
char Prefix[10];
//Call the function with Container_ID which has been declared as uint8. This is working.
CyBle_StoreAppData(Container_ID,datastack_ROM,10,0);
//Call the function with Prefix which has been declared as char. This is NOT working.
CyBle_StoreAppData(Prefix,datastack_ROM,10,0);
这是第二个电话的警告:
Here is the warning for the second call:
passing char[10] to parameter of type 'uint8 *' converts between pointers to integer types with different sign.
char
和uint8
不一样吗?
推荐答案
这两种类型的长度均为8位.区别在于签名.
Both types are 8bits long. The difference comes with signedness.
-
uint8
类型是无符号的. -
char
类型应根据您的情况进行签名.实际上,它取决于编译器,但是大多数编译器默认将char
类型视为带符号,并可以根据需要选择将char
类型强制为无符号.参见 C99标准文档参考§6.2 .5p15:
- The
uint8
type is unsigned. - The
char
type should be signed in your case. Actually, it is compiler dependent, but most compilers consider thechar
type as signed by default and have an option to forcechar
type as unsigned if needed. See the C99 standard document reference §6.2.5p15:
实现应将char定义为与签名char或未签名char具有相同的范围,表示形式和行为.
The implementation shall define char to have the same range, representation, and behavior as either signed char or unsigned char.
limits.h中定义的CHAR_MIN将具有值0或SCHAR_MIN之一,可用于区分这两个选项.
CHAR_MIN, defined in limits.h, will have one of the values 0 or SCHAR_MIN, and this can be used to distinguish the two options.
这篇关于uint8和C中的char之间的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!