如何将字符串转换为 IP 地址,反之亦然
如何转换字符串 ipAddress (struct in_addr) ,反之亦然? 如何提交未签名的长 ip 地址? 谢谢
how can I convert a string ipAddress (struct in_addr) and vice versa?
and how do I turn in unsigned long ipAddress?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
此示例显示如何从字符串转换为 ip,反之亦然:
This example shows how to convert from string to ip, and viceversa:
这里有易于使用、线程安全的 C++ 函数,用于将 uint32_t 本机端序转换为字符串,以及将字符串转换为本机端序 uint32_t:
公平警告,截至撰写本文时,它们尚未经过测试。但这些功能正是我来到这个线程时所寻找的。
here's easy-to-use, thread-safe c++ functions to convert uint32_t native-endian to string, and string to native-endian uint32_t:
fair warning, as of writing, they're un-tested. but these functions are exactly what i was looking for when i came to this thread.
十六进制 IP 地址到字符串 IP
输出:10.164.0.1
Hexadecimal IP Address to String IP
Output:10.164.0.1
第三个 inet_pton 参数是指向 in_addr 结构的指针。成功调用
inet_pton
后,in_addr
结构将填充地址信息。该结构的S_addr
字段包含网络字节顺序(逆序)的 IP 地址。The third
inet_pton
parameter is a pointer to anin_addr
structure. After a successfulinet_pton
call, thein_addr
structure will be populated with the address information. The structure'sS_addr
field contains the IP address in network byte order (reverse order).总结:
一步一步:
summarize :
step by step :
您可以使用两个函数
在 Windows 上在
winsock2.h
中定义,在 Linux 上在netdb.h
中定义•
WSAStringToAddressA
(用于将字符串转换为地址)•
WSAddressToStringA
(用于将地址转换为字符串)这两个函数最好的一点是它们适用于每个地址族
WSAStringToAddressA
该函数接受五个参数
•包含您的地址的数组或
char
数组的指针•您的地址的地址族(仅如果参数 3 为
NULL
,则使用)•
WSAPROTOCOL_INFO
结构提供有关您的协议的信息,如果要使用参数 2,则保留 null•指向
sockaddr< 结构的指针/code> 其中必须存储结果地址
•
DWORD
类型对象的指针,其中必须存储地址的结果长度WSAAddressToStringA
该函数还采用五个参数
• 指向必须翻译的
sockaddr
结构• 地址的字节长度
•
WSAPROTOCOL_INFO
结构提供有关地址协议的信息。使用NULL
使函数在参数1中使用sockaddr
结构的地址族。•
char
数组,其中字符串形式的地址具有要存储的地址的字符串形式的长度(以字节为单位)
示例
结果
127.0.0.1
You can use two functions
defined in
winsock2.h
on windows, innetdb.h
on Linux•
WSAStringToAddressA
(For converting a string to an address)•
WSAAddressToStringA
(For converting an address to a string)Best thing about these two functions is that they work for every address family
WSAStringToAddressA
This function takes to five arguments
•A pointer of array or array of
char
containing your address•Address family of your address(Only used if argument 3 is
NULL
)•
WSAPROTOCOL_INFO
structure providing information about your protocol, leave null if you want to use argument 2•Pointer to the structure of
sockaddr
in which the result address has to be stored•Pointer of the object of type
DWORD
in which the result length of your address has to be storedWSAAddressToStringA
This function also takes five arguments
•Pointer to the structure of
sockaddr
that has to be translated•Byte length of your address
•Structure of
WSAPROTOCOL_INFO
giving information about the address's protocol. UseNULL
to make the function use address family ofsockaddr
structure in argument 1•Array of
char
in which the address in the form of string has to be stored•Length of the string form of your address in bytes
Example
Result
127.0.0.1
在 PowerShell 中:
In PowerShell:
如果您需要其他方式,请使用
inet_ntop()
和inet_pton()
。不要使用inet_ntoa()、inet_aton()
及类似函数,因为它们已被弃用且不支持 ipv6。这是一个很好的指南,其中有很多示例。
Use
inet_ntop()
andinet_pton()
if you need it other way around. Do not useinet_ntoa(), inet_aton()
and similar as they are deprecated and don't support ipv6.Here is a nice guide with quite a few examples.
我不确定我是否正确理解了这个问题。
不管怎样,你在寻找这个:
输出:
I'm not sure if I understood the question properly.
Anyway, are you looking for this:
Output:
我能够使用以下代码将字符串转换为 DWORD 并返回:
我正在处理旧 MFC 代码的维护项目,因此转换已弃用的函数调用不适用。
I was able to convert string to DWORD and back with this code:
I am working in a maintenance project of old MFC code, so converting deprecated functions calls is not applicable.
inet_ntoa()
转换in_addr
到字符串:inet_addr()
执行相反的工作PS 这是谷歌搜索“in_addr to string”的第一个结果!
inet_ntoa()
converts ain_addr
to string:inet_addr()
does the reverse jobPS this the first result googling "in_addr to string"!
将字符串转换为 in-addr:
将 in_addr 转换为字符串:
To convert string to in-addr:
To convert in_addr to string: