格式化输出字符串,有符号整形和无符号整形

1. 最近遇到的问题,格式化输出的时候遇到了一个错误

char test = -106;
Report("test tlv:%d,%d,%d,%d",*o_val,(signed char)*o_val,test,(signed char)test);

看下report函数底层的代码

char log_pc_buff[128] = {0};
int Report(const char *pcFormat, ...)
{
    int iRet = 0;
    char *pcBuff, *pcTemp;
    int iSize = 512;
    va_list list;
    pcBuff = log_pc_buff;
    if(pcBuff == NULL)
    {
        return -1;
    }
    while(1)
    {
        va_start(list,pcFormat);
        iRet = vsnprintf(pcBuff,iSize,pcFormat,list);
        va_end(list);

输出的结果

test tlv:155,-101,150,-106

总结起来,底层的vsnprintf函数,如果不强制转换成有符号,那么就会出问题。

posted @ 2018-10-16 16:37  429512065  阅读(1303)  评论(0编辑  收藏  举报