C语言 在 C 中从 uint8_t * 转换为 char *
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42961443/
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
Convert from uint8_t * to char * in C
提问by skyleguy
I am programming in C using Atmel Studio (for those unfamiliar with this it is used to program to micro controllers such as arduino. You can not simply print, you have to send the data Serially to an application such as Terminal.)
我正在使用 Atmel Studio 用 C 编程(对于那些不熟悉它的人,它用于对 arduino 等微控制器进行编程。您不能简单地打印,您必须将数据串行发送到诸如终端之类的应用程序。)
I have so far:
我到目前为止:
uint8_t * stackHolder;
char buffer[128];
stackHolder = &buffer[127];
Lets say that the address of buffer[127]happens to be 0x207. I can see that the value of stackHolderis 0x207using the debugger. I have a function that takes a char *and prints that. So my question is how can I convert the uint8_t * stackHolderinto a char *so I can pass it to my print function?
假设 的地址buffer[127]恰好是0x207。我可以看到 的值stackHolder正在0x207使用调试器。我有一个接受 achar *并打印出来的函数。所以我的问题是如何将 the 转换uint8_t * stackHolder为 achar *以便将其传递给我的打印功能?
回答by emlai
回答by chux - Reinstate Monica
I have a method that takes a char *
我有一个方法需要一个字符 *
Suggest minimizing casts to one location to accomplish the goal and retain function signature type checking. Create a wrapper function with a cast.
建议尽量减少对一个位置的强制转换以实现目标并保留函数签名类型检查。使用强制转换创建包装函数。
// OP's original function
extern void skyleguy_Print(char *);
void skyleguy_Print_u8(uint8_t *s) {
skyleguy_Print((char *) s);
}

