include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
* #include <stdlib.h>
* unsigned long int strtoul(const char *nptr, char **endptr, int base);
* brief : 把字符串 nptr 里面的数字转换成 无符号长整数
* param[in] : nptr 原字符串
* param[in] : endptr 原字符串中被截取的字符串的下一个字符(截取长度跟base有关)
* param[in] : 转换的类型判断,范围为0(特殊值)、2-36;
* 0 函数自动判断字符串类型,并按10进制输出.
* e.g. 0x123ag -> 十六进制 0-f
* 01238 -> 八进制 0-7
* 其他类型除外,如 c,d等
* 2 合法字符: 0, 1 when base is 2, cover 0(2优先级高于0)
* 3 合法字符: 0, 1, 2 e.g. 01234(base=3) 1*3^1+2*3^0
* 4 合法字符: 0, 1, 2, 3 e.g. 01234(base=4) 1*4*4+2*4+3*4^0
* #include <ctype.h>
* int isxdigit(int c);
* brief : 检查所传的字符 c 是否是十六进制数字.
* return: 是,返回非零的整数值,否则返回 0.
*/
//十进制字符串转换为十六进制
static int decimal_to_hexbyte(const char* decimal_str, int decimal_to_len, char* hex, int* hex_len)
{
if (NULL == decimal_str || 0 == decimal_to_len) {
perror("decimal_to_hexbyte");
return -1;
}
if (decimal_to_len <= 10) {
char *pre_end = NULL;
int idata = strtoul((const char*)decimal_str, (char**)&pre_end, 10);
if (idata & 0xff000000) {
*hex_len = 4;
} else if (idata & 0xff0000) {
*hex_len = 3;
} else if (idata & 0xff00) {
*hex_len = 1;
}
memcpy(hex, &idata, *hex_len);
}
return 0;
(void)hex;
(void)hex_len;
}
int main()
{
char *p;
unsigned long int x = strtoul("0123abgt",&p,4);
printf("x::123abgt::%ld\n",x);
printf("p::%s\n", p);
return 0;
}
linux驱动之strtoul函数
最新推荐文章于 2024-06-28 16:40:10 发布