linux驱动之strtoul函数

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值