2014-12-19 103 views
-1

我不是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 
+1

和你的问题在_______行。? [请告诉我们您面临的问题。] – 2014-12-19 10:17:36

+0

使用*一个*缓冲区和一个*调用'sprintf'来一次性创建整个字符串,然后只调用一次* UART_write *。 – 2014-12-19 10:19:11

+1

有关工作代码的问题属于http://codereview.stackexchange.com – 2014-12-19 10:19:33

回答

1

可以减少所有这一切:

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); 

只是:

const char *msg = "[NETAPP EVENT] IP Acquired"; 
    const unsigned int ip_c_3 = SL_IPV4_BYTE(ipV4.ipV4, 3); 
    const unsigned int ip_c_2 = SL_IPV4_BYTE(ipV4.ipV4, 2); 
    const unsigned int ip_c_1 = SL_IPV4_BYTE(ipV4.ipV4, 1); 
    const unsigned int ip_c_0 = SL_IPV4_BYTE(ipV4.ipV4, 0); 
    char ip[256]; 
    sprintf(ip, "%s: %u.%u.%u.%u\n", msg, ip_c_3, ip_c_2, ip_c_1, ip_c_0); 

    UART_write(uart, ip, strlen(ip)); 

注:这也修复了原代码各种错误,包括在不需要的空格都被输入到输出端的问题。

+0

:你可能会在sprintf中使用unsigned char格式说明符,例如:http://stackoverflow.com/questions/27547377/format-specifier-for-unsigned-char – 2014-12-19 10:47:17

+1

@giorgim :是的,严格来说你可能是对的,但实际上我认为这没什么区别。我会改变类型。 – 2014-12-19 10:51:19

+0

谢谢保罗!也请加上;在第一行的结尾处 – Wuuzzaa 2014-12-19 11:40:16