十六进制IP为十进制IP转换 [英] Hex IP to Decimal IP conversion

查看:212
本文介绍了十六进制IP为十进制IP转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何转换为十六进制的IP(如 42477f35 ),并得到它之前吐出正确的十进制IP(在本例中,正确的结果是 66.71.127.53 )?

How can I convert a hex ip (such as 42477f35), and get it to spit out the correct decimal ip (in the example before, the correct result is 66.71.127.53)?

我想它采取十六进制IP作为一个字符串,然后再输出正确结果为一个字符串太让我可以在其他地方在C code使用它。

I'd like it to take the hex ip in as a string, then output the correct result as a string too so I can use it elsewhere in the C code.

我不是在C方面的专家,所以如果你们能帮助我了,这将会是AP preciated。

I'm not an expert in C, so if any of you can help me out, it'd be appreciated.

推荐答案

这是一种可能性:

#include <stdio.h>

int ip_hex_to_dquad(const char *input, char *output, size_t outlen)
{
    unsigned int a, b, c, d;

    if (sscanf(input, "%2x%2x%2x%2x", &a, &b, &c, &d) != 4)
        return -1;

    snprintf(output, outlen, "%u.%u.%u.%u", a, b, c, d);
    return 0;
}

这篇关于十六进制IP为十进制IP转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆