linux中字符串轉換函數 simple_strtoul【轉】


轉自:http://blog.csdn.net/tommy_wxie/article/details/7480087

Linux內核中提供的一些字符串轉換函數:

lib/vsprintf.c

[html] view plain copy
print?

     1. unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)    
     2. unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)    
     3. long simple_strtol(const char *cp, char **endp, unsigned int base)    
     4. long long simple_strtoll(const char *cp, char **endp, unsigned int base)    
     5. int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)    
     6. int strict_strtol(const char *cp, unsigned int base, long *res)    
     7. int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res)    
     8. int strict_strtoll(const char *cp, unsigned int base, long long *res)    
     9. int sprintf(char *buf, const char *fmt, ...)    
    10. int snprintf(char *buf, size_t size, const char *fmt, ...)    
    11. int sscanf(const char *buf, const char *fmt, ...)    


unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
功能:將一個字符串轉換成unsigend long long型數據。
返回:返回轉換后數據。

參數:cp指向字符串的開始,endp指向分析的字符串末尾的位置,base為要用的基數(進制數),base為0表示通過cp來自動判斷基數,函數自動可識別的基數:‘0x’表示16進制,‘0’表示8進制,其它都認定為10進制。函數可轉換成數字的有效字符為:[0,f]。舉例:cp = “0x12str”,base = 0,則返回unsigned long long為18,*endp = “str”。 參數下同。

[cpp] view plain copy
print?

    static ssize_t led_brightness_store(struct device *dev,  
            struct device_attribute *attr, const char *buf, size_t size)  
    {  
        struct led_classdev *led_cdev = dev_get_drvdata(dev);  
        ssize_t ret = -EINVAL;  
        char *after;  
        unsigned long state = simple_strtoul(buf, &after, 10);  
        size_t count = after - buf;  
      
        if (isspace(*after))  
            count++;  
      
        if (count == size) {  
            ret = count;  
      
            if (state == LED_OFF)  
                led_trigger_remove(led_cdev);  
            led_set_brightness(led_cdev, state);  
        }  
      
        return ret;  
    }  



unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
功能:將一個字符串轉換成unsigend long型數據。
返回:返回轉換后數據。

int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
功能:將一個字符串轉換成unsigend long型。
返回:轉換成功返回0,否則返回負。res指向轉換后的unsigned long數據。

說明:該函數對cp指向的字符串嚴格要求,cp指向的字符串必須為真正的unsigned long形式的字符串。字符串必須以“0x”、“0”、[0,f]開始,中間全部為有效的字符[0,f],否則返回為負。它會處理字符串最后的“\n”字符。下同


long long simple_strtoll(const char *cp, char **endp, unsigned int base)
功能:將一個字符串轉換成sigend long long型。
返回:返回轉換后數據。


int strict_strtol(const char *cp, unsigned int base, long *res)
功能:將一個字符串轉換sigend long型。
返回:轉換成功返回0,否則返回負。res指向轉換后的signed long數據。

int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res)
功能:將一個字符串轉換unsigend long long型。
返回:轉換成功返回0,否則返回負。res指向轉換后的unsigned long long數據。

int strict_strtoll(const char *cp, unsigned int base, long long *res)
功能:將一個字符串轉換sigend long long型。
返回:轉換成功返回0,否則返回負。res指向轉換后的signed long long數據。

int sprintf(char *buf, const char *fmt, ...)
功能:格式化輸出字符串,類似於printf,只是用字符串buf作為輸出對象。
返回:返回寫入buf字符串的字符個數。

int snprintf(char *buf, size_t size, const char *fmt, ...)
功能:格式化輸出字符串,類似於printf,只是用字符串buf作為輸出對象。其中size為buf的大小(包括‘\0’字符)。
返回:返回寫入buf字符串的字符個數。

int sscanf(const char *buf, const char *fmt, ...)
功能:格式化輸入字符串,類似於scanf,只是用字符串buf作為輸入對象。
返回:返回讀取buf字符串的字符個數。


lib/kasprintf

[cpp] view plaincopyprint?

    char *kasprintf(gfp_t gfp, const char *fmt, ...)  

[cpp] view plain copy
print?

    char *kasprintf(gfp_t gfp, const char *fmt, ...)  


char *kasprintf(gfp_t gfp, const char *fmt, ...)
功能:格式化輸出字符串到一段且gfp分配的內存中。
返回:返回指向該內容的字符串指針。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM