C語言對字串的一些操作

來源:互聯網
上載者:User

標籤:div   進位   move   class   span   i++   hex   字串   整數   

1. 字串中移除一個字元

 1 void removeChar(char *str, char c)  2 { 3     char *s = str; 4     int j, k; 5  6     for(j=k=0; s[j]!=‘\0‘; j++) { 7         if(s[j]!=c) 8             s[k++]=s[j]; 9     }10 11     s[k]= ‘\0‘;12 }

 

2. 字串轉為16進位整數

 1 unsigned int StrToHex(char *pszSrc, int nLen)   2 {   3     char h1, h2;   4     char s1, s2;  5     unsigned char pbDest; 6     unsigned int ret = 0; 7  8     //printf("%s, src: %s\n", __FUNCTION__, pszSrc); 9 10     for (int i = 0; i < nLen; i++)  11     {  12             h1 = pszSrc[2 * i];  13             h2 = pszSrc[2 * i + 1];  14       15             s1 = toupper(h1) - 0x30;  16             if (s1 > 9)  17                 s1 -= 7;  18       19             s2 = toupper(h2) - 0x30;  20             if (s2 > 9)  21                 s2 -= 7;  22             pbDest = s1 * 16 + s2;  23             //printf("h1: 0x%02x, h2: 0x%02x, s1: 0x%02x, s2: 0x%02x, dst: 0x%x\n", h1, h2, s1, s2, pbDest);  24 25             ret = (ret << 8) + pbDest;26             //printf("ret: 0x%x\n", ret);27     }  28 29     return ret;30 }  

 

C語言對字串的一些操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.