kernel 字符串切割,字符串转整型,整型转字符串

strsep(&data,","); //字符串切割函数
kstrtoint(first,10,&duty_cycle);//字符串转整形 10:十进制
sprintf(data,"%d,%d",duty_cycle,fan_freq);//整型数转字符串

示例代码:
ssize_t dcfan_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
{
	int ret;
	char *data;
	char *first,*second;
	
	printk(KERN_EMERG "%s,line:%d: enter\n",__FUNCTION__,__LINE__);
	
	if(!copy_from_user((char *)data, buf, count))
	{
		first	= strsep(&data,",");	//字符串切割函数
		second	= data;
		ret = kstrtoint(first,10,&duty_cycle);//字符串转整形 10:十进制
		if(ret)	return -1;
		ret = kstrtoint(second,10,&fan_freq);
		if(ret)	return -1;
		printk(KERN_EMERG "App write duty_cycle=%d,fan_freq=%d\n",duty_cycle,fan_freq);
		return count;
	}
	else
		return -1;
}

ssize_t dcfan_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
	size_t cnt;
	char data[10]={0};
	
	printk(KERN_EMERG "%s-line:%d: enter\n",__FUNCTION__,__LINE__);
	printk(KERN_EMERG "%s,duty_cycle=%d,fan_freq=%d\n",__FUNCTION__,duty_cycle,fan_freq);

	sprintf(data,"%d,%d",duty_cycle,fan_freq);//整型数转字符串

	cnt = strlen(data);
	if(!copy_to_user((char *)buf, data, cnt))
		return 0;
	else
		return -1;
}

你可能感兴趣的:(C语言)