本文介绍了Linux内核中如何将char[]字符串转换为int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
linux内核如何将char[]转为int
How convert char[] to int in linux kernel
验证输入的文本实际上是一个整数?
with validation that the text entered is actually an int?
int procfile_write(struct file *file, const char *buffer, unsigned long count,
void *data)
{
char procfs_buffer[PROCFS_MAX_SIZE];
/* get buffer size */
unsigned long procfs_buffer_size = count;
if (procfs_buffer_size > PROCFS_MAX_SIZE ) {
procfs_buffer_size = PROCFS_MAX_SIZE;
}
/* write data to the buffer */
if ( copy_from_user(procfs_buffer, buffer, procfs_buffer_size) ) {
return -EFAULT;
}
int = buffer2int(procfs_buffer, procfs_buffer_size);
return procfs_buffer_size;
}
推荐答案
在 在您友好的 linux 源代码树中.
See the various incarnations of kstrtol()
in #include <include/linux/kernel.h>
in your friendly linux source tree.
您需要哪个取决于 *buffer
是用户地址还是内核地址,以及您对错误处理/检查缓冲区内容的需求有多严格(例如,123qx
无效还是应该返回 123
?).
Which one you need depends on whether the *buffer
is a user or a kernel address, and on how strict your needs on error handling / checking of the buffer contents are (things like, is 123qx
invalid or should it return 123
?).
这篇关于Linux内核中如何将char[]字符串转换为int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!