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


    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函数,如果不强制转换成有符号,那么就会出问题。

  • 相关阅读:
    CodeForces
    CodeForces
    Simple Robot Gym
    Gym
    Java课程03总结
    Java课程03总结
    判断字符串是否回文
    Java课程02总结
    有关二进制原码、反码、补码
    Java课程01总结
  • 原文地址:https://www.cnblogs.com/429512065qhq/p/9798881.html
Copyright © 2020-2023  润新知