c/c++的一些函数使用方法记录

gethostbyname (很常用的域名解析函数,这里记录一下对解析后结果处理的一些方法)

inet_ntoa:将网络地址 转成 点分十进制 (也就是字符串 "192.168.xxx.xxx" 这种格式的)

inet_addr:将点分十进制的IP转换成一个长整数型数

这里附一个用长整型数的 ip 来判断当前ip是否为内网ip 的方法:

16字节对齐

#define MORE_ALIGN(__num, __x) (((__num)%(__x)) == 0 ? (__num) : ((((__num)/(__x))+1)*(__x)))

RGB颜色填充

#define RGB2PIXEL1555(r,g,b)    \
    ((((r) & 0xf8) << 7) | (((g) & 0xf8) << 2) | (((b) & 0xf8) >> 3) | 0x8000)
 

    uint16 BackColor = RGB2PIXEL1555(150, 150, 150);//毛玻璃黑白

#define RGB_TO_CRYCB(r, g, b)                                                            \
        (((unsigned int)(( 0.439f * (r) - 0.368f * (g) - 0.071f * (b)) + 128.0f)) << 16) |    \
        (((unsigned int)(( 0.257f * (r) + 0.564f * (g) + 0.098f * (b)) + 16.0f)) << 8) |        \
        (((unsigned int)((-0.148f * (r) - 0.291f * (g) + 0.439f * (b)) + 128.0f)))
       

获取本机ip

nic_eth_get_ipv4_addr("eth0", localIP, sizeof(localIP));

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <errno.h>


int nic_eth_get_ipv4_addr(const char *p_eth_name, char * p_ip_addr,int len)
{
	if (NULL == p_eth_name || strlen(p_eth_name) > IFNAMSIZ)	
	{		
		printf("%s:%s:%d, The net pNICName=%s is lenger than IFNAMSIZ=%d\n", __FUNCTION__, __FILE__, __LINE__, p_eth_name, IFNAMSIZ);		
		return 1;	
	}	

	int sockfd = socket (AF_INET, SOCK_DGRAM, 0);	
	if (sockfd < 0)
	{
		printf("%s:%s:%d, Create ipv4 socket error\n", __FUNCTION__, __FILE__, __LINE__);
		return 1;
	}

	struct ifreq ifr;  	
	memset(&ifr,0,sizeof(ifr));	
	strcpy(ifr.ifr_name, p_eth_name);  	
	if (0 > ioctl(sockfd, SIOCGIFFLAGS, (char *)&ifr))	
	{		
		close(sockfd);
		printf("%s:%s:%d, Get net device status failed:%s\n",__FUNCTION__, __FILE__,__LINE__, strerror(errno));		
		return 1;	
	}

	if(!(IFF_UP & ifr.ifr_flags))
	{
		close(sockfd);
		printf("%s:%s:%d, The net device is down\n", __FUNCTION__, __FILE__, __LINE__);
		return 1;
	}

	if (0 > ioctl(sockfd, SIOCGIFADDR, (char *)&ifr))		
	{			
		//printf("%s:%s:%d, Get ip address failed:%s\n",__FUNCTION__, __FILE__,__LINE__, strerror(errno));	
		close(sockfd);
		return 1;		
	}
	else		
	{	
		
		char *pIPTemp = inet_ntoa(((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr);	
		strncpy(p_ip_addr, pIPTemp, len);
		
		close(sockfd);
		
		return 0;		
	}
	

	
	return 0;

}


判断网线是否插入

nic_eth_is_nic_running("eth0");

bool nic_eth_is_nic_running(const char *p_eth_name)
{	

	if (NULL == p_eth_name || strlen(p_eth_name) > IFNAMSIZ)	
	{		
		printf("%s:%s:%d, The net pNICName=%s is lenger than IFNAMSIZ=%d\n", __FUNCTION__, __FILE__, __LINE__, p_eth_name, IFNAMSIZ);		
		return 1;	
	}

	int sockfd = socket (AF_INET, SOCK_DGRAM, 0);	
	if (sockfd < 0)
	{
		printf("%s:%s:%d, Create ipv4 socket error\n", __FUNCTION__, __FILE__, __LINE__);
		return 1;
	}
	
	struct   ifreq   ifr;  	
	memset(&ifr, 0, sizeof(ifr));	
	strcpy(ifr.ifr_name, p_eth_name);
	
	if (0 > ioctl(sockfd, SIOCGIFFLAGS, (char *)&ifr))	
	{
		close(sockfd);
		printf("%s:%s:%d, Get net device status failed:%s\n",__FUNCTION__, __FILE__,__LINE__, strerror(errno));	
		return false;
	}
	
	if((IFF_UP & ifr.ifr_flags) && ( IFF_RUNNING & ifr.ifr_flags ))	
	{	
		//printf("%s:%s:%d, net device is up\n",__FUNCTION__, __FILE__,__LINE__);	
		close(sockfd);
		return true;		
	}		
	else		
	{	
		//printf("%s:%s:%d, net device is down\n",__FUNCTION__, __FILE__,__LINE__);	
		close(sockfd);
		return false;		
	}	

	return false;

}

头文件

#include <stdio.h>
#include <string.h>
#include <net/if.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <linux/sockios.h>
 

c语言清屏

printf("\033[2J"); // 清屏

快速排序函数qsort

qsort (利用快速排序法排列数组)

相关函数 bsearch

定义函数 void qsort(void * base, size_t nmemb, size_t size, int (* compar) (const void *, const void *));

函数说明

参数base指向欲排序的数组开头地址,

参数nmemb代表数组中的元素数量, 每一元素的大小则由参数 size决定, 最后一项参数compar为一函数指针, 这个函数用来判断两个元素间的大小关系, 若传给compar的 第一个参数所指的元素数据大于第二个参数所指的元素数据则必须回传大于零的值, 两个元素数据相等则回传0.

🔴void * base : 指向了待排序数组的第一个元素(待排序数组的首地址)
🔴size_t num : 待排序的元素个数
🔴size_t size : 每个元素的大小(单位是字节)
🔴int ( * compar ) ( const void * , const void * ) : 是一个函数指针,指向一个函数,这个函数可以比较两个元素的大小

#include <stdio.h>
#include <stdlib.h> 
#define nmemb 7 
int compar(const void *a, const void *b) 
{ 
	int *aa = (int *)a, *bb = (int *)b; 
	if(*aa > *bb) 
		return 1; 
	if(*aa == *bb) 
		return 0; 
	if(*aa < *bb) 
		return -1; 
} 
int main() 
{ 
	int base[nmemb] = {3, 102, 5, -2, 98, 52, 18}; 
	int i; 
	for(i = 0; i < nmemb; i++) 
		printf("%d ", base[i]); 
	printf("\n"); 
	qsort(base, nmemb, sizeof(int), compar); 
	for(i = 0; i < nmemb; i++) 
		printf("%d ", base[i]); 
	printf("\n"); 
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值