[prototype]
long int atol ( const char * str );
[sample code]
#include
#include
int ch_num =0;
ch_num = atol (argv[0]);
/*此例子會把argv[0]的型態從string轉成int存放到ch_num去 [1]
*see Reference[1]
*/
-----------------------------------------------
[例2, 把IP位址從string型態轉成二進位的數字, argv[0]為string型態]
[prototype]
int inet_aton(const char * cp,struct in_addr *inp);
[sample code]
#include
#include
#include
int ch_num =0;
inet_aton(argv[0], &ch_num); //存放變數記得用pointer
/*
*此例子會把argv[0]的string型態從網路位址(ex.140.115.152.231)
*轉成二進制的數字
*延伸閱讀:inet_addr,inet_ntoa
*see Reference[2]
*/
-----------------------------------------------
[例3. convert address from presentation format to network format ]
[sample code]
#include "inet.h"
int ip=0;
inet_pton(AF_INET, argv[0], &ip);
/*
* 把argv[0]的string型態轉成ipv4的位址型態
* see reference[4]
*/
-----------------------------------------------
[例4. 把int 轉成 string] /* Convert integer to string (non-standard function) */
char * itoa ( int value, char * str, int base );
另一種用法為sprintf: [5]
ex.
- sprintf(str,"%d",value) converts to decimal base.
- sprintf(str,"%x",value) converts to hexadecimal base.
- sprintf(str,"%o",value) converts to octal base.
[例5. 把type ip4a 轉成char[ ] ] /* non-standard function) */
inet_ntoar(ip4a IP, char*[] )
ex.
ip4a IP;
char ip_str[INETADDR_LEN];
inet_ntoar(IP, ip_str);
References:
[1]http://www.cplusplus.com/reference/clibrary/cstdlib/atol/
[2]http://www.cs.nctu.edu.tw/~yslin/library/linuxc/function/14.html
[3]http://www.csie.ntnu.edu.tw/~u91029/libtech.html
[4]http://www.mkssoftware.com/docs/man3/inet_pton.3.asp
[5]http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/
沒有留言:
張貼留言