C ++如何将ip地址转换为字节? [英] C++ how to convert ip address to bytes?

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

问题描述

如何在C ++中将IP地址转换为字节?基本上如何解析IP地址?例如,如果我有一个等于 121.122.123.124 的字符串。
我需要解析这个,使 byte1 = 121 byte2 = 122 byte3 = 123 byte4 = 124

解决方案>

如果模式是常数,数字点数点等,然后使用istringstream:

  #include< sstream> 
using namespace std;

int byte1,byte2,byte3,byte4;
char dot;
char * ipaddress =121.122.123.124;
istringstream s(ipaddress); //现在包含ip地址字符串的输入流

s>> byte1>>点>> byte2>>点>> byte3>>点>> byte4>>点;


How would I convert an ip address into bytes in C++? Basically how do I parse the IP address? For example, if I have a string equal to 121.122.123.124. I need to parse this such that byte1 = 121, byte2 = 122, byte3 = 123, byte4 = 124.

解决方案

If the pattern is constant, number dot number dot etc, then use istringstream:

#include <sstream>
using namespace std;

int byte1, byte2, byte3, byte4;
char dot;
char *ipaddress = "121.122.123.124";
istringstream s(ipaddress);  // input stream that now contains the ip address string

s >> byte1 >> dot >> byte2 >> dot >> byte3 >> dot >> byte4 >> dot;

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

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