#define UNICODE #define _UNICODE #include <stdio.h> #include <windows.h> #include <tchar.h> bool IsHexChar(TCHAR c) { if( _T('a') <= c && c <= _T('f') || _T('A') <= c && c <= _T('F') || _T('0') <= c && c <= _T('9') ) return true; else return false; } void PrintfArray(long *lArray,int len) { for(int j=0;j<len;++j) { _tprintf(L"[%02d]:%8X \t",j,lArray[j]); if(j%6==5) _tprintf(L"\n"); } } int HexStr2Array( TCHAR *lpszHexBuffer, long *lArray ) { //第一次傳lArray=NULL可取得字符串中包含十六進制數值個數 TCHAR *pStart=0,*pEnd=0; int TotalArrayItem=0; pStart=lpszHexBuffer; int len=_tcslen(lpszHexBuffer); // printf("lpszHexBuffer=%p,%c\n",lpszHexBuffer,lpszHexBuffer[len-1]); while( *pStart!=TEXT('\0') && pStart<(lpszHexBuffer+len) )//while( *pStart!='\0' )pStart由_tcstol()返回時跳出,沒有找到\0,因此出現開頭箭頭所指的BUG { if( IsHexChar( *pStart ) ) { long ret = _tcstol(pStart, &pEnd/*返回指向第一個不能轉換的字符地址*/, 16/*字符串看成幾進*/); if( lArray!=NULL ) { lArray[ TotalArrayItem ]=ret; _tprintf(L"[%02d][%8X] %s\n",TotalArrayItem, ret ,pEnd ); } TotalArrayItem++; pStart=pEnd; } pStart++; } return TotalArrayItem; } int _tmain() { TCHAR *HexBuffer=L"^_^ieee-CCCC <*)>>>=< and --<-<-<@ 89a00b^-^I'll see what I can do.12 34 56 78 9a bc de f0^_^char0xface"; //TCHAR *HexBuffer=L"_1,2=3+4 5/6*7!8@9#0$a%b^c&d(e)f~"; int len= HexStr2Array( HexBuffer, NULL ); //lArray=NULL可取得字符串中包含十六進制數值個數 long *lArray=new long [len]; //long *lArray=(long*)malloc( len * sizeof( long ) );//sizeof( TCHAR )是錯的 _tprintf(L"HEX string: %s\n----------------\n",HexBuffer); for(int j=0;j<len;++j) { lArray[j]=0xcccccccc; } PrintfArray(lArray,len); _tprintf(L"\n----------------\n"); HexStr2Array( HexBuffer, lArray ); _tprintf(L"\n----------------\n"); PrintfArray(lArray,len); _tprintf(L"\n----------------\n"); ///////////////////////////////////////////////////////////////////////////////////////// if( lArray ) { delete [] lArray; lArray=0; } //if(lArray) //{ // free(lArray); // lArray=0; //} }
下面以類方式建立
windows
//#define UNICODE //#define _UNICODE #include <stdio.h> #include <windows.h> #include <tchar.h> class HexStringConvertor { private: bool IsHexChar(TCHAR c)//c爲十六進字符返回true,不然返回false { if( TEXT('a') <= c && c <= TEXT('f') || TEXT('A') <= c && c <= TEXT('F') || TEXT('0') <= c && c <= TEXT('9') ) { return true; } else { return false; } } int HexStrScan( long *m_lArray ) { //第一次傳m_lArray=NULL可取得字符串中包含十六進制數值個數 TCHAR *pStart=0,*pEnd=0;//pStrat初始指向m_lpszHexBuffer,用於遍歷;pEnd於_tcstol()中返回,指向第一個不能轉換的字符地址 int iArrayItemMax=0;//iArrayItemMax爲十六進數個數的計數器 int lenHexBuffer=lstrlen(m_lpszHexBuffer); pStart=m_lpszHexBuffer; while( pStart <lenHexBuffer+m_lpszHexBuffer ) // error: while( *pStart!=TEXT('\0') ) { if( IsHexChar( *pStart ) ) { long lret = _tcstol(pStart, &pEnd/*返回指向第一個不能轉換的字符地址*/, 16/*字符串看成幾進*/); if( m_lArray!=NULL ) //第二次傳m_lArray!=NULL將取得十六進制數值存入m_lArray { m_lArray[ iArrayItemMax ]=lret; _tprintf(TEXT("[%02d][%8X] pStart:%p(%c),pEnd:%p(%c)\n"),iArrayItemMax, lret ,pStart,*pStart,pEnd,*pEnd );//%x不要用於輸出地址,要用%p代 } iArrayItemMax++; pStart=pEnd; } pStart++; } return iArrayItemMax; } void HexStringInit(TCHAR *lpszHexBuffer ) { m_lpszHexBuffer=lpszHexBuffer; _tprintf(TEXT("\n------------HexStringInit() AND HexStrScan()----------------\n")); _tprintf( TEXT("HexString:\"%s\"\n"), m_lpszHexBuffer ); if( m_lArray ) { delete [] m_lArray; m_lArray=0; } m_TotalArrayItem= HexStrScan( NULL ); //參數傳NULL可取得字符串中包含十六進制數值個數 m_lArray=new long[ m_TotalArrayItem ]; HexStrScan( m_lArray ); } public: void SetString(TCHAR *lpszHexBuffer ) { HexStringInit( lpszHexBuffer ); } HexStringConvertor( TCHAR *lpszHexBuffer ) { m_lArray=0;//必上, HexStringInit( lpszHexBuffer ); } ~HexStringConvertor() { if( m_lArray ) { delete [] m_lArray; m_lArray=0; } m_lpszHexBuffer=NULL; } int size(void) { return m_TotalArrayItem; } long operator[](int index) const { return m_lArray[ index ]; } void PrintfArray(void) { _tprintf(TEXT("PrintfArray():\n")); for(int j=0;j<m_TotalArrayItem;++j) { _tprintf( TEXT("[%02d]:%8X \t"),j,m_lArray[j]); if(j%6==5) _tprintf(TEXT("\n")); } _tprintf(TEXT("\n") ); } void CheckChar(void) { for(int i=0;i<lstrlen(m_lpszHexBuffer);++i) { _tprintf(TEXT("[%03d]: %c ->"),i,m_lpszHexBuffer[i] ); IsHexChar(m_lpszHexBuffer[i])?_tprintf( TEXT("true\n") ):_tprintf( TEXT("false\n") ); } } private: int m_TotalArrayItem; long *m_lArray; TCHAR *m_lpszHexBuffer; }; int _tmain() { HexStringConvertor s= TEXT("ghikjlmnopqrstuvwxyz!!#@$@%$@%&*&*$&#)$0s0xfa<-") ; _tprintf(TEXT("\nTEST size(),operator[]:\n")); for(int i=0;i<s.size();++i) { _tprintf( TEXT("(i:%02d)[%X]\t"),i,s[i]); if(i%6==5) _tprintf(TEXT("\n")); } _tprintf(TEXT("\n")); s.PrintfArray(); TCHAR *s0=TEXT("^_^ieee-CCCC 0 0xa31E <*)>>>=< and --<-<-<@ 89a00b^-^I'll see what I can do.12 34 56 78 9a bc de f0^_^char0xface"); s.SetString( s0 ); s.PrintfArray(); s.SetString( TEXT("_1,2=3+4 5/6*7!8@9#0$a%b^c&d(e)f~") ); s.PrintfArray(); s.SetString( TEXT("0X10x20x30xa0xb 0xc0xd0xe\00xf") ); s.PrintfArray(); }