【问题标题】:C - unsigned char * to charC - unsigned char * to char
【发布时间】:2014-12-19 10:16:11
【问题描述】:

我不是 C 方面的专家,对字符转换有一些理解问题。我写了一些运行良好的代码,但我不太喜欢它......

我希望你们能想出一个更好、更优化的版本。我也得到这样的输出:NETAPP EVENT] IP Acquired: 192.168.0__.111 (_ = space)。当只有一位数字时,如何摆脱空格?

谢谢

嵌入式工作编码器将 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

【问题讨论】:

  • 你的问题在 _______ 行。? [请告诉我们您面临的问题。]
  • 仅使用 one 缓冲区和 one 调用 sprintf 一次性创建整个字符串,然后仅调用 UART_write 一次
  • 关于工作代码的问题属于codereview.stackexchange.com
  • @Wuuzzaa:你确定你的sprintf 工作正常吗?你用三个元素写入数组。如果整数是 3 位长怎么办?目标数组中是否有空终止符(我猜 sprintf 会附加)的空间?
  • @Wuuzzaa:它可能“看起来”可以工作,但是sprintf 代码是错误的,那里有缓冲区溢出

标签: c embedded rtos


【解决方案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));

注意:这还修复了原始代码中的各种错误,包括在输出中引入不需要的空格的问题。

【讨论】:

  • :您可能在 sprintf 中为 unsigned char 使用了错误的格式说明符,例如 stackoverflow.com/questions/27547377/…
  • @giorgim:是的,严格来说你可能是正确的,但实际上我认为这没有任何区别。不过我会更改类型。
  • 谢谢保罗!也请补充;在第一行的末尾
猜你喜欢
  • 2011-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-04
  • 1970-01-01
  • 2013-07-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多