[英]How do I store an unsigned short into a char array? C language
I'm a beginner in C language, I was wondering how I store an unsigned short into a char array? 我是C语言的初学者,我想知道如何将unsigned short存储到char数组中?
unit16_t is unsigned short, and below is my code. unit16_t是unsigned short,下面是我的代码。
uint16_t combined = 1234;
char echoPrompt[] = {combined};
EDIT: 编辑:
Sorry for being unclear I need echoPrompt to be a char array and combined needs to be an integer. 抱歉,不清楚,我需要echoPrompt是一个char数组,并且合并后必须是一个整数。 I am passing echPrompt as a char array to a UART_write which required a char array.
我将echPrompt作为char数组传递给需要char数组的UART_write。
You cannot pass an array to a function. 您不能将数组传递给函数。 You can pass a pointer.
您可以传递一个指针。 Pointers are not arrays.
指针不是数组。
If your UART_write looks anything like any of the standard C/POSIX functions write
, fwrite
etc, you need 如果您的UART_write看起来像任何标准C / POSIX函数
write
, fwrite
等,则需要
result = UART_write (...,
(char*)&combined,
sizeof(combined), ...);
There is no need in a char array. 不需要char数组。
if I understood correctly, you want to save each upper and lower 1 byte of short in char array. 如果我理解正确,您想将short的每个高1和低1字节保存在char数组中。
well, why don't you just use union? 好吧,为什么不使用工会呢?
typedef struct uint16_s{
char upper;
char lower;
} uint16_s;
typedef union uint16_u{
uint16_s sBuf;
uint16_t tBuf;
} uint16_u;
uint16_u combined;
combined.tBuf = 1234;
char echoPrompt[] = {combined.sBuf.upper, combined.sBuf.lower};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.