Linux中字符串轉整形數字的函數

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

lib/vsprintf.c

 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”。 參數下同。



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字符串的字符個數。



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字符串的字符個數。

發佈了93 篇原創文章 · 獲贊 44 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章