天天看點

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

繼續閱讀