简体   繁体   English

在u中从uint8_t *转换为char *

[英]Convert from uint8_t * to char * in C

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 . 可以说buffer[127]的地址恰好是0x207 I can see that the value of stackHolder is 0x207 using the debugger. 我可以看到使用调试器的stackHolder的值是0x207 I have a function that takes a char * and prints that. 我有一个函数,它采用char *并打印出来。 So my question is how can I convert the uint8_t * stackHolder into a char * so I can pass it to my print function? 所以我的问题是如何将uint8_t * stackHolder转换为char *所以我可以将它传递给我的print函数?

How can I convert the uint8_t * stackHolder into a char * ? 如何将uint8_t * stackHolder转换为char *

By casting : 通过铸造

print((char*) stackHolder);

I have a method that takes a char * 我有一个方法,需要一个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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM