网络字节转换函数--IP地址转换--字符->int_32
2013-05-07 16:08
281 查看
inet_aton,inet_addr,inet_ntoa在点分十进制数串(如,“192.168.1.10")与他的32位网络字节二进制值之前转换IPV4地址,有2个比较新的函数inet_pton和inet_ntop,这2个对IPV4和IPV6地址都能处理
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int inet_aton(const char *cp, struct in_addr *inp);
in_addr_t inet_addr(const char *cp);我就是用这个才弄好的
char *inet_ntoa(struct in_addr in);
inet_aton() converts the Internet host address cp from the standard
numbers-and-dots notation into binary data and stores it in the struc‐
ture that inp points to. inet_aton() returns non-zero if the address is
valid, zero if not.
inet_aton() 转换网络主机地址cp为二进制数值,并存储在struct in_addr结构中,即第二个参数*inp,函数返回非0表示cp主机有地有效,返回0表示主机地址无效。
The inet_addr() function converts the Internet host address cp from
numbers-and-dots notation into binary data in network byte order. If
the input is invalid, INADDR_NONE (usually -1) is returned. This is an
obsolete interface to inet_aton(), described immediately above; it is
obsolete because -1 is a valid address (255.255.255.255), and
inet_aton() provides a cleaner way to indicate error return.
inet_addr函数转换网络主机地址(如192.168.1.10)为网络字节序二进制值,如果参数char *cp无效,函数返回-1(INADDR_NONE),这个函数在处理地址为255.255.255.255时也返回-1,255.255.255.255是一个有效的地址,不过inet_addr无法处理;
The inet_ntoa() function converts the Internet host address in given in
network byte order to a string in standard numbers-and-dots notation.
The string is returned in a statically allocated buffer, which subse‐
quent calls will overwrite.
inet_ntoa 函数转换网络字节排序的地址为标准的ASCII以点分开的地址,,该函数返回指向点分开的字符串地址的指针,该字符串的空间为静态分配的,这意味着在第二次调用该函数时,上一次调用将会被重写(复盖),所以如果需要保存该串最后复制出来自己管理!
现在一般使用inet_aton和inet_ntoa来处理网络字节和主机字节之间的转换;
有两个更新的函数inet_pton和inet_ntop这2个函数能够处理ipv4和ipv6,原型如下
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int inet_pton(int af, const char *src, void *dst);
这个函数转换字符串到网络地址,第一个参数af是地址族,转换后存在dst中
inet_pton 是inet_addr的扩展,支持的多地址族有下列:
AF_INET
src为指向字符型的地址,即ASCII的地址的首地址(ddd.ddd.ddd.ddd格式的),函数将该地址
转换为in_addr的结构体,并复制在*dst中
AF_INET6
src为指向IPV6的地址,,函数将该地址
转换为in6_addr的结构体,并复制在*dst中
如果函数出错将返回一个负值,并将errno设置为EAFNOSUPPORT,如果参数af指定的地址族和src格式不对,函数将返回0。
函数inet_ntop进行相反的转换原型如下
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt);
这个函数转换网络二进制结构到ASCII类型的地址,参数的作用和上面相同,只是多了一个参数socklen_t cnt,他是所指向缓存区dst的大小,避免溢出,如果缓存区太小无法存储地址的值,则返回一个空指针,并将errno置为ENOSPC
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int inet_aton(const char *cp, struct in_addr *inp);
in_addr_t inet_addr(const char *cp);我就是用这个才弄好的
char *inet_ntoa(struct in_addr in);
inet_aton() converts the Internet host address cp from the standard
numbers-and-dots notation into binary data and stores it in the struc‐
ture that inp points to. inet_aton() returns non-zero if the address is
valid, zero if not.
inet_aton() 转换网络主机地址cp为二进制数值,并存储在struct in_addr结构中,即第二个参数*inp,函数返回非0表示cp主机有地有效,返回0表示主机地址无效。
The inet_addr() function converts the Internet host address cp from
numbers-and-dots notation into binary data in network byte order. If
the input is invalid, INADDR_NONE (usually -1) is returned. This is an
obsolete interface to inet_aton(), described immediately above; it is
obsolete because -1 is a valid address (255.255.255.255), and
inet_aton() provides a cleaner way to indicate error return.
inet_addr函数转换网络主机地址(如192.168.1.10)为网络字节序二进制值,如果参数char *cp无效,函数返回-1(INADDR_NONE),这个函数在处理地址为255.255.255.255时也返回-1,255.255.255.255是一个有效的地址,不过inet_addr无法处理;
The inet_ntoa() function converts the Internet host address in given in
network byte order to a string in standard numbers-and-dots notation.
The string is returned in a statically allocated buffer, which subse‐
quent calls will overwrite.
inet_ntoa 函数转换网络字节排序的地址为标准的ASCII以点分开的地址,,该函数返回指向点分开的字符串地址的指针,该字符串的空间为静态分配的,这意味着在第二次调用该函数时,上一次调用将会被重写(复盖),所以如果需要保存该串最后复制出来自己管理!
现在一般使用inet_aton和inet_ntoa来处理网络字节和主机字节之间的转换;
有两个更新的函数inet_pton和inet_ntop这2个函数能够处理ipv4和ipv6,原型如下
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
int inet_pton(int af, const char *src, void *dst);
这个函数转换字符串到网络地址,第一个参数af是地址族,转换后存在dst中
inet_pton 是inet_addr的扩展,支持的多地址族有下列:
AF_INET
src为指向字符型的地址,即ASCII的地址的首地址(ddd.ddd.ddd.ddd格式的),函数将该地址
转换为in_addr的结构体,并复制在*dst中
AF_INET6
src为指向IPV6的地址,,函数将该地址
转换为in6_addr的结构体,并复制在*dst中
如果函数出错将返回一个负值,并将errno设置为EAFNOSUPPORT,如果参数af指定的地址族和src格式不对,函数将返回0。
函数inet_ntop进行相反的转换原型如下
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt);
这个函数转换网络二进制结构到ASCII类型的地址,参数的作用和上面相同,只是多了一个参数socklen_t cnt,他是所指向缓存区dst的大小,避免溢出,如果缓存区太小无法存储地址的值,则返回一个空指针,并将errno置为ENOSPC
相关文章推荐
- 【Java.IO】I/O 字节 <-> 字符转换流 —— InputStreamReader,OutputStreamWriter(FileReader,FileWriter)
- 问题1:java中没有实现这种“byte a = 0xB2 --> String b = “B2””转换的简单实现需要自己实现。 答:自己编写的转换函数,思路将byte的高低4位分开,分别转换为对应的字符然后合成返回的字符串。 java 代码 1.
- <Win32 API> 多字节字符与宽字符 WideCharToMultiByte/MultiByteToWideChar
- ASCII+Unicode+UTF8(字符编码方式->字节) Base64(字节编码方式->64字符)
- <ctype.h> 字符测试函数:字符大小转换函数
- 数据库字段内容存在'、"、>、<等特殊字符,转义html字符输出
- ios网络请求特殊字符&处理
- 字符串转成:字符+字符出现次数 。如1233422222->1121324125
- &nbsp|&quot|&amp|&lt|&gt等html字符转义
- linux网络编程常用函数详解与实例(socket-->bind-->listen-->accept)
- javaWeb开发中的字符过滤器配置--->让你二步轻松告别乱码
- <C/C++>C语言字节对齐问题总结
- boost中类似于 shared_array<int> ptr(0) error
- 7、利用泛型和反射,让public static <T> T findById(Class<T> clazz, int id)方法,更具一般性
- &nbsp|&quot|&amp|&lt|&gt等html字符转义
- 错误 1 error LNK2019: 无法解析的外部符号 "public: __thiscall chain<int>::~chain<int>(void)" (??1?$chain@H@@QAE@
- varchar是按字节存储的.而带"n"的nvarchar是按字符存储的
- 第16周 项目2-去除字符串str1中的特定字符(空格),结果仍保存在原字符串中<指针做参数>