我不是C方面的专家,并且对字符转换有一些理解问题。我写了一些代码工作得很好,但我不喜欢那么多...C - 无符号字符*到字符
我希望你们能想出一个更好,更优化的版本。此外,我得到这样的输出:NETAPP EVENT] IP获取:192.168.0 __。111(_ =空格)。如果只有一位数字,我怎么能摆脱这些空间?
感谢
嵌入式工作编码器来打印PIv4地址到串行控制台:
...
//Print IP in UART
sl_NetCfgGet(SL_IPV4_STA_P2P_CL_GET_INFO, &dhcpIsOn, &len,(unsigned char *)&ipV4);
const unsigned char ip_c_3 = SL_IPV4_BYTE(ipV4.ipV4, 3);
char ip_3[3];
sprintf(ip_3,"%ld", (int)ip_c_3);
const unsigned char ip_c_2 = SL_IPV4_BYTE(ipV4.ipV4, 2);
char ip_2[3];
sprintf(ip_2,"%ld", (int)ip_c_2);
const unsigned char ip_c_1 = SL_IPV4_BYTE(ipV4.ipV4, 1);
char ip_1[3];
sprintf(ip_1,"%ld", (int)ip_c_1);
const unsigned char ip_c_0 = SL_IPV4_BYTE(ipV4.ipV4, 0);
char ip_0[3];
sprintf(ip_0,"%ld", (int)ip_c_0);
UART_write(uart,"[NETAPP EVENT] IP Acquired: ",sizeof("[NETAPP EVENT] IP Acquired: "));
UART_write(uart,ip_3, sizeof(ip_3));
UART_write(uart,".",1);
UART_write(uart,ip_2,sizeof(ip_2));
UART_write(uart,".",1);
UART_write(uart,ip_1,sizeof(ip_1));
UART_write(uart,".",1);
UART_write(uart,ip_0,sizeof(ip_0));
UART_write(uart,"\n",2);
...
UART_write:
/*!
* @brief Function that writes data to a UART
*
* This function initiates an operation to write data to a UART controller.
*
* In UART_MODE_BLOCKING, UART_write will block task execution until all
* the data in buffer has been written.
*
* In UART_MODE_CALLBACK, UART_write does not block task execution an calls a
* callback function specified by writeCallback.
*
* @param handle A UART_Handle
*
* @param buffer A pointer to buffer containing data to be written
*
* @param size The number of bytes in buffer that should be written
* onto the UART.
*
* @return Returns the number of bytes that have been written to the UART,
* UART_ERROR on an error.
*/
extern int UART_write(UART_Handle handle, const void *buffer, size_t size);
sl_NetCfgGet:
/*!
\brief Internal function for getting network configurations
\return On success, zero is returned. On error, -1 is
returned
\param[in] ConfigId configuration id
\param[out] pConfigOpt Get configurations option
\param[out] pConfigLen The length of the allocated memory as input, when the
function complete, the value of this parameter would be
the len that actually read from the device.\n
If the device return length that is longer from the input
value, the function will cut the end of the returned structure
and will return ESMALLBUF
\param[out] pValues - get configurations values
\sa
\note
\warning
\par
\sample code
SL_IPV4_AP_P2P_GO_GET_INFO:
Get static IP address for AP or P2P go.
_u8 len = sizeof(SlNetCfgIpV4Args_t);
_u8 dhcpIsOn = 0; // this flag is meaningless on AP/P2P go.
SlNetCfgIpV4Args_t ipV4 = {0};
sl_NetCfgGet(SL_IPV4_AP_P2P_GO_GET_INFO,&dhcpIsOn,&len,(_u8 *)&ipV4);
printf("IP %d.%d.%d.%d MASK %d.%d.%d.%d GW %d.%d.%d.%d DNS %d.%d.%d.%d\n",
SL_IPV4_BYTE(ipV4.ipV4,3),SL_IPV4_BYTE(ipV4.ipV4,2),SL_IPV4_BYTE(ipV4.ipV4,1),SL_IPV4_BYTE(ipV4.ipV4,0),
SL_IPV4_BYTE(ipV4.ipV4Mask,3),SL_IPV4_BYTE(ipV4.ipV4Mask,2),SL_IPV4_BYTE(ipV4.ipV4Mask,1),SL_IPV4_BYTE(ipV4.ipV4Mask,0),
SL_IPV4_BYTE(ipV4.ipV4Gateway,3),SL_IPV4_BYTE(ipV4.ipV4Gateway,2),SL_IPV4_BYTE(ipV4.ipV4Gateway,1),SL_IPV4_BYTE(ipV4.ipV4Gateway,0),
SL_IPV4_BYTE(ipV4.ipV4DnsServer,3),SL_IPV4_BYTE(ipV4.ipV4DnsServer,2),SL_IPV4_BYTE(ipV4.ipV4DnsServer,1),SL_IPV4_BYTE(ipV4.ipV4DnsServer,0));
\endcode
*/
#if _SL_INCLUDE_FUNC(sl_NetCfgGet)
_i32 sl_NetCfgGet(_u8 ConfigId ,_u8 *pConfigOpt, _u8 *pConfigLen, _u8 *pValues);
#endif
和你的问题在_______行。? [请告诉我们您面临的问题。] – 2014-12-19 10:17:36
使用*一个*缓冲区和一个*调用'sprintf'来一次性创建整个字符串,然后只调用一次* UART_write *。 – 2014-12-19 10:19:11
有关工作代码的问题属于http://codereview.stackexchange.com – 2014-12-19 10:19:33