1. /*@author: lgh@
  2. *
  3. * */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #include <netinet/in.h>//ntohl
  9. #include <arpa/inet.h> //inet_addr
  10. #include <sys/types.h>
  11. #ifdef UNICODE
  12. #define T(x) __TXT(x)
  13. #define __TXT(s) L ## s
  14. #else
  15. #define T(s) s
  16. #endif /* UNICODE */
  17. /*
  18. #if defined (DEBUG_DISABLE)
  19. #undef DEBUG_DISABLE
  20. #else
  21. #endif
  22. */
  23.  
  24. /*********************************************************************/
  25. #ifndef DEBUG_DISABLE
  26. /*
  27. #define lprint(tag, fmt...) \
  28. do{if(tag)printf(fmt);}while(0)
  29. */
  30. #define lprint(x, ...) do{printf(x, ##__VA_ARGS__);}while(0)
  31. #else
  32. #define lprint(tag, fmt...)
  33. #endif
  34.  
  35. /*********************************************************************/
  36. #ifdef CONFIG_DUAL_IMAGE
  37. #if defined (CONFIG_RT2880_FLASH_2M)
  38. #define IMAGE1_SIZE 0x100000
  39. #elif defined (CONFIG_RT2880_FLASH_4M)
  40. #define IMAGE1_SIZE 0x200000
  41. #elif defined (CONFIG_RT2880_FLASH_8M)
  42. #define IMAGE1_SIZE 0x400000
  43. #elif defined (CONFIG_RT2880_FLASH_16M)
  44. #define IMAGE1_SIZE 0x800000
  45. #elif defined (CONFIG_RT2880_FLASH_32M)
  46. #define IMAGE1_SIZE 0x1000000
  47. #else
  48. #define IMAGE_SIZE TEST
  49. #endif
  50. #endif//end #ifdef,否则会出现25:1: error: unterminated #ifdef,这样的错
  51.  
  52. /*********************************************************************/
  53. typedef void* (*square_t)(int* x);
  54.  
  55. void* square (int* x)
  56. {
  57. *x *= *x;
  58. return (void*)square;
  59. }
  60.  
  61. int lt1224(void)
  62. {
  63. int x = ;
  64. ((square_t)((square_t)((square_t)square (&x)) (&x)) (&x)) (&x);
  65. printf ("%d\n", x);
  66. return ;
  67. }
  68. /*********************************************************************/
  69. #ifndef NIP4
  70. #define NIP4_FMT "%u.%u.%u.%u"
  71. #define NIP4(addr) \
  72. ((unsigned char *)&addr)[], \
  73. ((unsigned char *)&addr)[], \
  74. ((unsigned char *)&addr)[], \
  75. ((unsigned char *)&addr)[]
  76.  
  77. #define HIP4_FMT "%u.%u.%u.%u"
  78. #define HIP4(addr) \
  79. ((unsigned char *)&addr)[], \
  80. ((unsigned char *)&addr)[], \
  81. ((unsigned char *)&addr)[], \
  82. ((unsigned char *)&addr)[]
  83. #endif
  84.  
  85. #ifndef NIP6
  86. #define NIP6_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x"
  87. #define NIP6(addr) \
  88. (unsigned)((addr)[]), \
  89. (unsigned)((addr)[]), \
  90. (unsigned)((addr)[]), \
  91. (unsigned)((addr)[]), \
  92. (unsigned)((addr)[]), \
  93. (unsigned)((addr)[]), \
  94. (unsigned)((addr)[]), \
  95. (unsigned)((addr)[]), \
  96. (unsigned)((addr)[]), \
  97. (unsigned)((addr)[]), \
  98. (unsigned)((addr)[]), \
  99. (unsigned)((addr)[]), \
  100. (unsigned)((addr)[]), \
  101. (unsigned)((addr)[]), \
  102. (unsigned)((addr)[]), \
  103. (unsigned)((addr)[])
  104. #endif
  105.  
  106. /*
  107. #ifdef __CHECKER__
  108. #define __bitwise__ __attribute__((bitwise))
  109. #else
  110. #define __bitwise__
  111. #endif
  112. #ifdef __CHECK_ENDIAN__
  113. #define __bitwise __bitwise__
  114. #else
  115. #define __bitwise
  116. #endif
  117. typedef __u16 __bitwise __le16;
  118. typedef __u16 __bitwise __be16;
  119. typedef __u32 __bitwise __le32;
  120. typedef __u32 __bitwise __be32;
  121. typedef __u64 __bitwise __le64;
  122. typedef __u64 __bitwise __be64;
  123. */
  124.  
  125. typedef __signed__ char __s8;
  126. typedef unsigned char __u8;
  127.  
  128. typedef __signed__ short __s16;
  129. typedef unsigned short __u16;
  130.  
  131. typedef __signed__ int __s32;
  132. typedef unsigned int __u32;
  133.  
  134. typedef __signed__ long __s64;
  135. typedef unsigned long __u64;
  136.  
  137. union nf_inet_addr {
  138. __u32 all[];
  139. __u32 ip;// __be32 ip;
  140. // __be32 ip6[4];
  141. struct in_addr in;
  142. struct in6_addr in6;
  143. };
  144. /*********************************************************************/
  145.  
  146. union endian{
  147. int i;
  148. char c;
  149. };
  150.  
  151. int main(int argc, char *argv[])
  152. {
  153. char *s="";
  154. char *p;
  155. p=strpbrk(s,"a1 839"); /*1会最先在s字符串中找到*/
  156. lprint("%s\n",p);
  157. p=strpbrk(s,"");/*3 会最先在s 字符串中找到*/
  158. lprint("%s\n",p);
  159. lprint("%s\n", T("deling"));
  160.  
  161. lprint("************************************************************************\n");
  162. lt1224();
  163.  
  164. lprint("************************************************************************\n");
  165. if(argc != )
  166. {
  167. return ;
  168. }
  169. union endian le;
  170. le.i = 0x3456;/*16bit宽的数*/
  171. if(le.c == 0x56){
  172. lprint("little endian\n");
  173. }else lprint("big endian\n");
  174. printf("%0x, %0x\n", le.i, le.c);
  175.  
  176. char ipdot[] = {};/*点分十进制*/
  177. long iphost = ;/*主机字节序,x86一般都是小端模式*/
  178. long ipnet = ;/*网络字节序,大端模式*/
  179.  
  180. strncpy(ipdot, argv[], sizeof(ipdot));
  181. lprint("Get ipdot:%s\n", ipdot);
  182.  
  183. ipnet = inet_addr(ipdot);
  184. lprint("Get ipnet:%ld:"NIP4_FMT"\n", ipnet, NIP4(ipnet));
  185. iphost = ntohl(ipnet);
  186. lprint("Get iphost:%ld:"NIP4_FMT"\n", iphost, NIP4(iphost));
  187. lprint("Get iphost:%ld:"HIP4_FMT"\n", iphost, HIP4(iphost));
  188. iphost++;
  189. lprint("Get iphost++:%ld:"HIP4_FMT"\n", iphost, HIP4(iphost));
  190.  
  191. /*net --> host*/
  192. unsigned array[];
  193. array[] = (ntohl(ipnet) >> ) & 0xFF;
  194. array[] = (ntohl(ipnet) >> ) & 0xFF;
  195. array[] = (ntohl(ipnet) >> ) & 0xFF;
  196. array[] = ntohl(ipnet) & 0xFF;
  197. lprint(NIP4_FMT"\n", array[], array[], array[], array[]);
  198.  
  199. lprint("%ld.%ld.%ld.%ld\n", ipnet & 0xff,
  200. ipnet>> & 0xff,
  201. ipnet>> & 0xff,
  202. ipnet>> & 0xff);
  203. lprint("************************************************************************\n");
  204. return ;
  205. }

ip地址转化代码实例的更多相关文章

  1. C#验证ip地址的代码

    如下的代码是关于C#验证ip地址的代码. public Boolean CheckIPValid(String strIP) { char chrFullStop = '.'; string[] ar ...

  2. JSP 获取真实IP地址的代码

    [转载]JSP 获取真实IP地址的代码 JSP 获取真实IP地址的代码 在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的.   ...

  3. 【转载】JSP 获取真实IP地址的代码

    JSP 获取真实IP地址的代码 在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的.   但是在通过了 Apache,Squid ...

  4. Linux下获取本机IP地址的代码

    Linux下获取本机IP地址的代码,返回值即为互联网标准点分格式的字符串. #define ETH_NAME "eth0" //获得本机IP地址 char* GetLocalAdd ...

  5. js获取IP地址多种方法实例教程

    js获取IP地址方法总结   js代码获取IP地址的方法,如何在js中取得客户端的IP地址.原文地址:js获取IP地址的三种方法 http://www.jbxue.com/article/11338. ...

  6. 将IP地址转化为整数

    $ip = 'IP地址';echo $intip = sprintf('%u',ip2long($ip)); //转换为无符号整型echo long2ip($intip);//将整型转换为ip

  7. 获取Linux下的IP地址 java代码

    /** * 获取Linux下的IP地址 * * @return IP地址 * @throws SocketException */ public static String getLinuxLocal ...

  8. C#获取本地IP地址[常用代码段]

    获得当前机器的IP代码,假设本地主机为单网卡 string strHostName = Dns.GetHostName(); //得到本机的主机名 IPHostEntry ipEntry = Dns. ...

  9. openresty(nginx)中使用lua脚本获取请求IP地址的代码

    人狠话不多,直接上代码:------------------------------------------------------------------------------------- lo ...

随机推荐

  1. C#局域网桌面共享软件制作(一)

    很久没有写文章了,今天分享的内容是局域网桌面共享软件,相信很多人都做过,也可能遇到一些问题,这里我将一一测试. 涉及到的内容有:局域网文件传输.桌面截屏.图片压缩等.为了更直白的演示程序的功能所以没有 ...

  2. linux下vi的复制,黏贴,删除,撤销,跳转等命令

    前言     在嵌入式linux开发中,进行需要修改一下配置文件之类的,必须使用vi,因此,熟悉 vi 的一些基本操作,有助于提高工作效率. 一,模式vi编辑器有3种模式:命令模式.输入模式.末行模式 ...

  3. Yii框架中使用PHPExcel导出Excel文件

    最近在研究PHP的Yii框架,很喜欢,碰到导出Excel的问题,研究了一下,就有了下面的方法: 1.首先在config\main.php中添加对PHPExcel的引用,我的方式是这样: 1 2 3 4 ...

  4. caused by android.system.errnoexception open failed eacces (permission denied)解决方案,安卓6.0(API23)权限问题

    在API23+以上,不止要在AndroidManifest.xml里面添加权限 <uses-permission android:name="android.permission.RE ...

  5. 9)Java内部类(Inner Class)

      内部类:不可以有静态数据,静态方法或者又一个静态内部类      内部类的优点:隐藏类的细节,内部类可以声明为私有.内部类可以访问外部类的对象(包括private) 静态内部类:可以有静态数据,静 ...

  6. masterha_check_repl报错汇总

    [root@DBMysql ~]#masterha_check_repl --conf=/etc/masterha/app1.cnf 导致如下报错的原因主要有两类: 1.mysql的安装时用源码安装, ...

  7. 第十六章 调试及安全性(In .net4.5) 之 调试程序

    1. 概述 本章内容包括 如何选择合适的构建类型.创建和管理编译指令.管理程序数据文件(pdb)和指令. 2. 主要内容 2.1 构建类型 .net中默认的两种生成模式是 发布(Release)模式 ...

  8. 正确打印含unicode字符的dict

    python中,dict 对象转换为类似为 \UXXXX 的格式:这种格式英文称为 code point,完全看不懂:当然,也可以通过这个网站来转换 http://rishida.net/tools/ ...

  9. WebService到底是什么?(转)

    一.序言 大家或多或少都听过WebService(Web服务),有一段时间很多计算机期刊.书籍和网站都大肆的提及和宣传WebService技术,其中不乏很多吹嘘和做广告的成分.但是不得不承认的是Web ...

  10. 利用HttpModule开发asp.net页面、ashx等访问时session失效的统一处理入口

    web程序时,当使用session时总会出现失效而报“未将对象引用设置到对象的实例”的http 500错误,本人比较懒,不想每个地方都用try catch处理,就找到个用httpModule统一处理的 ...