在C ++中将十六进制IP转换为十进制IP

时间:2015-04-22 06:23:05

标签: c++ linux ip hex

如何在Linux中使用C ++将hex格式的IP(例如0101007F)转换为Dotted Decimal

1 个答案:

答案 0 :(得分:1)

您可以尝试这样:

static char* hexdecimal_to_decimalip(const char *in)
{
    char *out = (char*)malloc(sizeof(char) * 16);
    unsigned int p, q, r, s;

    if (sscanf(in, "%2x%2x%2x%2x", &p, &q, &r, &s) != 4)
        return out;
    sprintf(out, "%u.%u.%u.%u", p, q, r, s);
    return out;
}