linux中字串轉換函式 simpl

2021-08-15 17:58:43 字數 3578 閱讀 4174

linux核心中提供的一些字串轉換函式:

lib/vsprintf.c

[html]

view plain

copy

print?

1. unsigned long long ******_strtoull(const char *cp, char **endp, unsigned int base) 

2. unsigned long ******_strtoul(const char *cp, char **endp, unsigned int base) 

3. long ******_strtol(const char *cp, char **endp, unsigned int base) 

4. long long ******_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 ******_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) 

return

ret; } 

unsigned long ******_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 ******_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 plain

copy

print?

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分配的記憶體中。

返回:返回指向該內容的字串指標。

字串轉換函式

標頭檔案 stdlib.h 1 函式名 atof 功 能 把字串轉換成浮點數 用 法 double atof const char nptr 函式說明 atof 會掃瞄引數nptr字串,跳過前面的空格字元,直到遇上數字或正負符號才開始做轉換,而再遇到非數字或字串結束時 0 才結束 轉換,並將結果返回...

字串轉換函式

itoa,是廣泛應用的非標準c語言擴充套件函式。將任意型別的數字轉換為字串。在中與之有相反功能的函式是atoi。由於它不是標準c語言函式,所以不能在所有的編譯器中使用。功能 將任意型別的數字轉換為字串。在中與之有相反功能的函式是atoi。char itoa int value,char string...

字串函式 將字串轉換數字

標頭檔案 includeatof 函式用來將字串轉換成雙精度浮點數 double 函式說明 atof 會掃瞄引數nptr字串,跳過前面的空格字元,直到遇上數字或正負符號才開始做轉換,而再遇到非數字或字串結束時 0 才結束轉換,並將結果返回,str字串可包含正負號 小數點或e e 來表示指數部分 in...