如何在C ++中将整数数组转换为IP地址? [英] How to convert an integer array into an ip address in c++?

查看:64
本文介绍了如何在C ++中将整数数组转换为IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组ipAddress [4],其中包含4个整数值.127,0,0,1,我需要将其转换为字符串"127.0.0.1".

I have an array ipAddress[4] with 4 integer values e.g. 127,0,0,1 and I need to convert it to a string "127.0.0.1".

我该怎么做?

为澄清起见,我稍后需要将此功能与功能输入进行比较.我不想在屏幕上打印出来.

Just to clarify, I need this at a later point to compare with an input from a function. I do not want to print this out on the screen.

推荐答案

一种方法是:

char buf[16];
snprintf( buf, sizeof buf, "%d.%d.%d.%d",
    ipAddress[0], ipAddress[1], ipAddress[2], ipAddress[3] );

我个人觉得这比涉及循环和多个函数调用的10行解决方案更为优雅,其他人会不同意!

Personally I find this more elegant than 10-line solutions involving loops and multiple function calls, others will disagree!

这篇关于如何在C ++中将整数数组转换为IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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