c\c++ 中字符串分割,并且转换为整形数据

  在项目开发中,经常使用到字符串分割, 并且将其转换为整形(比如IP的分割获取,MAC地址的分割获取等),代码如下:

#ifndef _UNICODE
    void StrToIntData( char *pStr,int nIntData[],const char * pTok )
    {

        char *pStrPos = NULL;
        char *pNextToken = NULL;
        int nPos = 0;

        pStrPos = strtok_s(pStr,pTok,&pNextToken);

        while (pStrPos) 
        {
            nIntData[nPos] = atoi(pStrPos);
            nPos ++;
            pStrPos = strtok_s(NULL,pTok,&pNextToken);
        } 

    }

    char * IntToStrData(int nData)
    {
        static char str[MAX_PATH] = {0};
        itoa(nData,str,10); 
        return str;
    }

#else
    void StrToIntData( wchar_t *pStr,int nIntData[],const wchar_t * pTok )
    {

        wchar_t *pStrPos = NULL;
        wchar_t *pNextToken = NULL;
        int nPos = 0;

        pStrPos = wcstok_s(pStr,pTok,&pNextToken);

        while (pStrPos) 
        {
            nIntData[nPos] = _ttoi(pStrPos);
            nPos ++;
            pStrPos = wcstok_s(NULL,pTok,&pNextToken);
        } 

    }

    wchar_t * IntToStrData(int nData)
    {
        static  wchar_t str[MAX_PATH] = {0};
        _itot(nData,str,10); 
        return str; 
    }

#endif

 

#include <locale.h>  
#include <Shlwapi.h>  
  
#pragma comment(lib,"Shlwapi.lib")  
  
void CharNormalOp();  
void WcharNoramlOp();  
  
int main(int argc,char *argv[])  
{  
    setlocale(LC_ALL,"chs");    
  
    CharNormalOp();  
    printf("\r\n");  
    WcharNoramlOp();  
}  
  
void CharNormalOp()  
{  
    char str1[MAX_PATH] = {'\0'};  
    char str2[MAX_PATH] = {'\0'};  
  
    //strcpy   
    strcpy(str2,strcpy(str1,"abcde@fgh@tjk@lmn@123009333"));  
    puts(str1);  
    puts(str2);  
  
    //strcat  
    strcat(str2,strcat(str1,"##kkkmmm##"));  
    puts(str1);  
    puts(str2);  
  
    //strcmp  
    if(0 == strcmp(str1,str2))  
    {  
        puts("str1 equals str2");  
    }  
  
    //忽略大小写比较  
    if(0 == stricmp(str1,str2))  
    {  
        puts("str1 eaquals str2 when ignore case");  
    }  
  
    //字符串查找  
    if(strstr(str2,str1))  
    {  
        puts("str2 contains str1");  
    }  
  
    //字符串忽略大小写查找  
    if(StrStrIA(str1,"ABCD"))  
    {  
        puts("str1 contains abcd when ignore case");  
    }  
  
    //分割字符串  
    char *cur = str2;  
    char *p = strtok(cur,"@");  
    while(p)  
    {  
        printf("sub string = %s\r\n",p);  
        cur += (strlen(p)+strlen("@"));  
        p = strtok(cur,"@");  
    }  
}  
  
void WcharNoramlOp()  
{  
    WCHAR str1[MAX_PATH] = {L'\0'};  
    WCHAR str2[MAX_PATH] = {L'\0'};  
  
    //wcscpy   
    wcscpy(str2,wcscpy(str1,L"我们@学习@发展@KK3@123"));  
    printf("str1=%S\r\n",str1);  
    printf("str2=%S\r\n",str2);  
  
    //strcat  
    wcscat(str2,wcscat(str1,L"##km环境##"));  
    printf("str1=%S\r\n",str1);  
    printf("str2=%S\r\n",str2);  
  
    //strcmp  
    if(0 == wcscmp(str1,str2))  
    {  
        printf("str1 equals str2\r\n");  
    }  
  
    //忽略大小写比较  
    if(0 == wcsicmp(str1,str2))  
    {  
        printf("str1 eaquals str2 when ignore case\r\n");  
    }  
  
    //字符串查找  
    if(wcsstr(str2,str1))  
    {  
        printf("str2 contains str1\r\n");  
    }  
  
    //字符串忽略大小写查找  
    if(StrStrIW(str1,L"kk3"))  
    {  
        printf("str1 contains kk3 when ignore case\r\n");  
    }  
  
    //分割字符串  
    WCHAR *cur = str2;  
    WCHAR *p = wcstok(cur,L"@");  
    while(p)  
    {  
        printf("sub wstring = %S\r\n",p);  
        cur += (wcslen(p)+wcslen(L"@"));  
        p = wcstok(cur,L"@");  
    }  
  
}  

原贴地址:http://blog.csdn.net/faithzzf/article/details/51984197

  

posted @ 2017-02-09 11:00  公子綦  阅读(2229)  评论(0编辑  收藏  举报