String.cpp
上传用户:hbzxgg2
上传日期:2009-03-28
资源大小:291k
文件大小:4k
- #include "StdAfx.h"
- #include "String.h"
- CString Data2HexString ( char *data, int size, int nBytesPerLine/*=16*/ )
- {
- CString csHexString, csTemp;
- for ( int i=0; i<size; i++ )
- {
- csTemp.Format ( L"%02X ", (BYTE)data[i] );
- csHexString += csTemp;
- if ( (i+1) % nBytesPerLine == 0 )
- csHexString += "rn";
- }
- return csHexString;
- }
- BYTE HexCharToData(TCHAR cChar)
- {
- if ( cChar >= _T('0') && cChar <= _T('9') )
- return (BYTE)(cChar-_T('0'));
- if ( cChar >= _T('a') && cChar <= _T('f') )
- return (BYTE)(cChar-_T('a')+0x0a);
- if ( cChar >= _T('A') && cChar <= _T('F') )
- return (BYTE)(cChar-_T('A')+0x0A);
- return 0xff;
- }
- //
- // 将一个16进制的字符串转换为16进制的数,返回数据长度
- // 例如:将字符串“61 62 63 64 65 66 67 68”转换成数据 [0x61 0x62 0x63 0x64 0x65 0x66 0x67 0x68]
- // 保存到 szOutBuffer 缓冲中
- //
- int ConvStringToHexData(CString csOrgStr, BYTE *szOutBuffer, int nOutBufferSize)
- {
- ASSERT ( szOutBuffer && nOutBufferSize > 0 );
- // 先得到有效的16进制字符串,同时将表示16进制的标志“0x”去掉
- CString csLegalHexStr;
- csOrgStr.MakeLower();
- csOrgStr.Replace ( _T("0x"), _T("") );
- for ( int i=0; i<csOrgStr.GetLength(); i++ )
- {
- TCHAR c = csOrgStr.GetAt (i);
- if ( (c>=_T('0')&&c<=_T('9')) || (c>=_T('a') && c<= _T('f')) )
- {
- csLegalHexStr += c;
- }
- }
- if ( csLegalHexStr.GetLength() < 1 ) return 0;
- // 不是偶数个字符则在后面补“0”
- if ( (csLegalHexStr.GetLength() % 2) != 0 )
- csLegalHexStr += _T("0");
- int j=0;
- for ( int i=0; j<nOutBufferSize && i<csLegalHexStr.GetLength(); i+= 2 )
- {
- BYTE btData = 0;
- BYTE bLo = ( 0x0f & HexCharToData ( csLegalHexStr.GetAt(i+1) ) );
- BYTE bHi = ( 0x0f & HexCharToData ( csLegalHexStr.GetAt(i) ) );
- if ( bLo == 0xff || bHi == 0xff )
- return -1;
- btData = ( bHi << 4 ) | bLo;
- szOutBuffer[j++] = btData;
- }
- return j;
- }
- //
- // 将 lpszOrg 转换为多字节表示的字符串
- //
- int GetMltiByteChar ( LPCTSTR lpszOrg, OUT char *buf, int size )
- {
- if ( !lpszOrg || !buf || size<1 ) return -1;
- #ifdef UNICODE
- return WideCharToMultiByte ( CP_ACP, 0, lpszOrg, (int)strlen_s(lpszOrg), (LPSTR)buf, size, NULL, NULL );
- #else
- strncpy_s ( buf, lpszOrg, size-1 );
- return strlen_s(buf);
- #endif
- }
- //
- // 将 lpszOrg 转换为该程序使用的编码字符串,如果该程序是 UNICODE 就转为 UNICODE,如果是 ANSI 就转为 ANSI 的
- //
- CString GetCompatibleString ( LPCTSTR lpszOrg, BOOL bOrgIsUncode )
- {
- if ( !lpszOrg ) return L"";
- #ifdef UNICODE
- if ( bOrgIsUncode ) return lpszOrg;
- int nOrgLen = strlen((const char*)lpszOrg);
- int nWideCount = nOrgLen + 1;
- WCHAR *wchar = new WCHAR[nWideCount];
- if ( !wchar ) return L"";
- memset ( wchar, 0, nWideCount*sizeof(WCHAR) );
- MultiByteToWideChar(CP_ACP, 0, (LPCSTR)lpszOrg, nOrgLen, wchar, nWideCount);
- CString csRet = wchar;
- delete[] wchar;
- return csRet;
- #else
- if ( !bOrgIsUncode ) return lpszOrg;
- int nOrgLen = wcslen(lpszOrg);
- int nMultiByteSize = nOrgLen / 2 + 2;
- CString csRet
- GetMltiByteChar ( lpszOrg, csRet.GetBuffer(nMultiByteSize), nMultiByteSize );
- csRet.ReleaseBuffer();
- return csRet;
- #endif
- }
- int hwSnprintf ( LPTSTR buffer, int count, LPCTSTR format, ... )
- {
- if ( count < 1 ) return 0;
- ASSERT_ADDRESS ( buffer, count );
- memset ( buffer, 0, count );
- // 格式化
- va_list va;
- va_start (va, format);
- #ifdef UNICODE
- int nRet = _vsnwprintf ( buffer, count, (const wchar_t*)format, va);
- #else
- int nRet = _vsnprintf ( buffer, count, (const char*)format, va);
- #endif
- va_end(va);
- buffer [count-1] = _T(' ');
- int nLen = nRet;
- if ( nLen < 0 ) nLen = (int)strlen_s(buffer);
- if ( nLen > count ) nLen = count;
- return nLen;
- }