16进制整数转化成字符串


    Linux下gcc没有itoa,项目要用到16位地址转换成字符通过串口发送,只能自己动手
                              


int itoa(int val, char *buf,int size) const//16进制
{
    char *p = buf;
    char t = '0';
    int len = 0;
    int slen = size;
    while(val > 0 && len < slen) {
        t = val % 16;
        val -= t;
        val /= 16;
        if (t > 9) {
            *p++ = t- 10 + 'a';
        } else {
            *p++ = t + '0';
        }
        ++len;
    }
    for (int i = 0;i < len/2;++i) {
        char c = buf[i];
        buf[i] = buf[len-1-i];
        buf[len-1-i] = c;
    }
    buf[len] = '\0';
    return len;
}


你可能感兴趣的:(C/C++)