C函数加密实现及常用字符串处理函数的使用
realpath函數
realpath是用來將參數path所指的相對路徑轉換成絕對路徑,然后存于參數resolved_path所指的字符串數組或指針中的一個函數。 如果resolved_path為NULL,則該函數調用malloc分配一塊大小為PATH_MAX的內存來存放解析出來的絕對路徑,并返回指向這塊區域的指針。程序員應調用free來手動釋放這塊內存 #include <stdlib.h> 函數原型:char *realpath(const char *path, char *resolved_path) 返回值: 成功則返回指向resolved_path的指針,失敗返回NULL,錯誤代碼存于errno 范例: #include <unistd.h> main() {char resolved_path[80];realpath("/usr/X11R6/lib/modules/../../include/../",resolved_path);printf("resolved_path: %s\n", resolved_path); }getopt函數
getopt(分析命令行參數)
相關函數
表頭文件 #include
神奇的字符分割函數strtok()函數
例子1:
#include<string.h> #include<stdio.h> int main(void) {char input[16]="abc,d";char*p;/*strtok places a NULL terminatorinfront of the token,if found*/p=strtok(input,",");if(p)printf("%s\n",p);/*Asecond call to strtok using a NULLas the first parameter returns a pointerto the character following the token*/p=strtok(NULL,",");if(p)printf("%s\n",p); return 0; }例子2:
#include<string.h> #include<stdio.h> int main(void) {char input[16]="abc,123,e,f";char*p;/*strtok places a NULL terminatorinfront of the token,if found*/p=strtok(input,",");if(p)printf("%s\n",p);/*Asecond call to strtok using a NULLas the first parameter returns a pointerto the character following the token*/p=strtok(NULL,",");if(p)printf("%s\n",p);p=strtok(NULL,",");if(p)printf("%s\n",p);p=strtok(NULL,",");if(p)printf("%s\n",p);return 0; }輸出:
/home/andrew/文檔/untitled5/cmake-build-debug/untitled5 abc 123 e fProcess finished with exit code 0C++例子:
#include<iostream> #include<cstring> using namespace std; int main() {char sentence[]="This is a sentence with 7 tokens";cout << "The string to be tokenized is:\n" << sentence << "\n\nThe tokens are:\n\n";char *tokenPtr=strtok(sentence," ");while(tokenPtr!=NULL) {cout<<tokenPtr<<'\n';tokenPtr=strtok(NULL," ");}//cout << "After strtok,sentence=" << tokenPtr<<endl;return 0; }strtod函數
strtod是C語言及C++中的重要函數,功能是將字符串轉換成浮點數,表頭文件是#include
相關 atoi,atol,strtod,strtol,strtoul 表頭文件 #include <stdlib.h> 定義函數 double strtod(const char *nptr,char **endptr);strtod()會掃描參數nptr字符串,跳過前面的空格字符,直到遇上數字或正負符號才開始做轉換,到出現非數字或字符串結束時(‘\0’)才結束轉換,并將結果返回。若endptr不為NULL,則會將遇到不合條件而終止的nptr中的字符指針由endptr傳回。參數nptr字符串可包含正負號、小數點或E(e)來表示指數部分。如123.456或123e-2。
小測試
#include<stdlib.h> #include<stdio.h> void main() {char *endptr;char a[] = "asd12345.6789";char b[] = "1234.567qwer";char c[] = "-232.23e4";printf( "a=%lf\n", strtod(a+3,NULL) );printf( "b=%lf\n", strtod(b,&endptr) );printf( "endptr=%s\n", endptr );printf( "c=%lf\n", strtod(c,NULL) ); }輸出
/home/andrew/文檔/untitled5/cmake-build-debug/untitled5 a=12345.678900 b=1234.567000 endptr=qwer c=-2322300.000000Process finished with exit code 0atoi函數
**
atoi (表示 ascii to integer)是把字符串轉換成整型數的一個函數,應用在計算機程序和辦公軟件中。atoi( ) 函數會掃描參數 nptr字符串,跳過前面的空白字符(例如空格,tab縮進等。
可以通過isspace( )函數來檢測),直到遇上數字或正負符號才開始做轉換,而在遇到非數字或字符串結束時(‘\0’)才結束轉換,并將結果返回。如果 nptr不能轉換成 int 或者 nptr為空字符串,那么將返回 0[1] 。
**
atol函數
**
函數名: atol
功 能: 把字符串轉換成長整型數
用 法: long atol(const char *nptr);
**
表頭文件: #include
#include <stdlib.h> #include <stdio.h> int main(void) {long l;char *str = "98765432";l = atol(str); /* 原來為l = atol(str); */printf("string = %s integer = %ld\n", str, l);return(0); } 執行 string=98765432 integer=98765432 #include<stdlib.h> main() {char a[]=”1000000000”;char b[]=” 234567890”;long c;c=atol(a)+atol(b);printf(“c=%d\n”,c); } 執行 c=1234567890## isdigit()函數 ##
isdigit是計算機C(C++)語言中的一個函數,主要用于檢查參數是否為十進制數字字符。用于判斷字符c是否為數字,當c為數字0~9時,返回非零值,否則返回零。 可以用一個字符數組循環判斷每一項是否為數字
## 解開strtoul的神秘面紗 ##
strtoul,將參數nptr字符串根據參數base來轉換成無符號的長整型數。
strtoul (將字符串轉換成無符號長整型數)
相關函數
atof,atoi,atol,strtod,strtol
unsigned long strtoul(const char *nptr,char **endptr,int base);
【參數說明】str 為要轉換的字符串,endstr 為第一個不能轉換的字符的指針,base 為字符串 str 所采用的進制。
【函數說明】strtoul() 會將參數 str 字符串根據參數 base 來轉換成無符號的長整型數(unsigned long)。參數 base 范圍從2 至36,或0。參數 base 代表 str 采用的進制方式,如 base 值為10 則采用10 進制,若 base 值為16 則采用16 進制數等。
溫馨提示:ANSI C 規范定義了 stof()、atoi()、atol()、strtod()、strtol()、strtoul() 共6個可以將字符串轉換為數字的函數,大家可以對比學習。另外在 C99 / C++11 規范中又新增了5個函數,分別是 atoll()、strtof()、strtold()、strtoll()、strtoull()
**參數1:字符串起始地址
參數2:返回字符串有效數字的結束地址,這也是為什么要用二級指針的原因。
參數3:轉換基數。當base=0,自動判斷字符串的類型,并按10進制輸出,例如”0xa”,就會把字符串當做16進制處理,輸出的為10。更多的下面詳細闡述。**
strtoul()會將參數nptr字符串根據參數base來轉換成無符號的長整型數。參數base范圍從2至36,或0。參數base代表采用的進制方式,如base值為10則采用10進制,若base值為16則采用16進制數等。當base值為0時會根據情況選擇用哪種進制:如果第一個字符是’0’,就判斷第二字符如果是‘x’則用16進制,否則用8進制;第一個字符不是‘0’,則用10進制。一開始strtoul()會掃描參數nptr字符串,跳過前面的空格字符串,直到遇上數字或正負符號才開始做轉換,再遇到非數字或字符串結束時(”)結束轉換,并將結果返回。若參數endptr不為NULL,則會將遇到不合條件而終止的nptr中的字符指針由endptr返回。
返回值
返回轉換后的長整型數,否則返回ERANGE并將錯誤代碼存入errno中。
附加說明
ERANGE指定的轉換字符串超出合法范圍。
例子中間變量是存放第一個不能轉換的變量的地址
#include <stdio.h> #include <stdlib.h> int main () {char szNumbers[] = "2001 60c0c0 -1101110100110100100000 0x6fffff";char * pEnd;long int li1, li2, li3, li4;li1 = strtol (szNumbers,&pEnd,10);li2 = strtol (pEnd,&pEnd,16);li3 = strtol (pEnd,&pEnd,2);li4 = strtol (pEnd,NULL,0);printf ("轉換成10進制: %ld、%ld、%ld、%ld\n", li1, li2, li3, li4);return 0; }輸出結果:
/home/andrew/文檔/untitled5/cmake-build-debug/untitled5 轉換成10進制: 2001、6340800、-3624224、7340031Process finished with exit code 0總結
以上是生活随笔為你收集整理的C函数加密实现及常用字符串处理函数的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 高级C语言教程编码风格
- 下一篇: 【2016年第4期】突发大数据在存储辅助