C语言 将 unsigned char * (uint8_t *) 转换为 const char *
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7009231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cast unsigned char * (uint8_t *) to const char *
提问by Lo?c G.
I've a function which take an uint8_t * argument :
我有一个带有 uint8_t * 参数的函数:
uint8_t* ihex_decode(uint8_t *in, size_t len, uint8_t *out)
{
uint8_t i, hn, ln;
for (i = 0; i < len; i+=2) {
hn = in[i] > '9' ? (in[i]|32) - 'a' + 10 : in[i] - '0';
ln = in[i+1] > '9' ? (in[i+1]|32) - 'a' + 10 : in[i+1] - '0';
out[i/2] = (hn << 4 ) | ln;
}
return out;
}
I use this function with :
我将此功能用于:
uint8_t data[SPM_PAGESIZE]; // SPM_PAGESIZE = 256 bytes
uint8_t sysex_data[SPM_PAGESIZE/2];
ihex_decode(data, strlen(data), sysex_data);
But in this case, my compiler (avr-gcc) return a warning :
但在这种情况下,我的编译器 (avr-gcc) 返回警告:
main.c|89|warning: pointer targets in passing argument 1 of 'strlen' differ in signedness /usr/include/string.h|399|note: expected 'const char *' but argument is of type 'uint8_t *'
main.c|89|警告:'strlen' 传递参数 1 中的指针目标的符号不同 /usr/include/string.h|399|注意:预期为 'const char *' 但参数类型为 'uint8_t *'
So, i've found a solution by type casting the data var :
所以,我通过类型转换数据 var 找到了一个解决方案:
ihex_decode(data, strlen((const char *)data), sysex_data);
The warning disappears but I wonder if this solution is safe.
警告消失了,但我想知道这个解决方案是否安全。
Is there a better way ?
有没有更好的办法 ?
Thanks
谢谢
采纳答案by Diego Sevilla
It is safe. The error has to do with mixing unsigned integers of 8 bits with characters, that are signed if you use just char.
这是安全的。该错误与将 8 位无符号整数与字符混合有关,如果您只使用char.
I see, however, that the function accepts uint8_tand does character arithmetic, so it should accepts chars (or const chars, for the matter). Note that a character constant 'c'is of type char, and you're mixing signed and unsigned in the expressions inside ihex_decode, so you have to be careful to avoid overflows or negative numbers treated as big positive numbers.
然而,我看到该函数接受uint8_t并执行character算术,因此它应该接受chars(或const chars,就此而言)。请注意,字符常量'c'的类型为char,并且您在 内部的表达式中混合了有符号和无符号ihex_decode,因此您必须小心避免溢出或将负数视为大正数。
A last style note. As inis not modified, it should read const uint8_t* in(or const char* in, as of above) in the parameters. Another style error (that may lead to very bad errors) is that you accept lenas size_t, but declare the iloop variable as uint8_t. What if the string is more than 255 bytes in length?
最后的样式说明。由于in未修改,它应该在参数中读取const uint8_t* in(或const char* in如上所述)。另一个样式错误(可能会导致非常严重的错误)是您接受lenas size_t,但将i循环变量声明为uint8_t。如果字符串的长度超过 255 个字节怎么办?
回答by Patrick B.
Everything which is non-const * can be safely casted to const * in C. It's save.
所有非常量 * 都可以安全地转换为 C 中的 const *。这是保存。
回答by the_nic
This is safe. The warning (I think) pops up because you're casting from unsigned to signed.
这是安全的。警告(我认为)弹出是因为您正在从未签名转换为已签名。
回答by Kamath
Its safe, the range of char < uint8_t.
它的安全,char < uint8_t 的范围。

