myutil 20170602

Posted micq

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了myutil 20170602相关的知识,希望对你有一定的参考价值。

#pragma once
#include "P645Define.h"

#define MYUTIL_VERSION            20170602

class myutil
{
public:
    myutil(void);
    virtual ~myutil(void);

    static BOOL  IsIntInRange(int value, int min, int max);
    static void  SetIntInRange(int &value, int minval, int maxval);
    static BOOL  IsUlongInRange(unsigned long value, unsigned long min, unsigned long max);
    static void  SetIntInRange(unsigned long &value, unsigned long minval, unsigned long maxval);
    static void  IntTimeToSystemTime(unsigned long nTime, SYSTEMTIME &sTime);
    static void  SystemTimeToIntTime(SYSTEMTIME sTime, unsigned long &nTime);
    static BOOL  OpenFileDialog(HWND hWnd, LPTSTR sOutFilename, int nOutSize, LPCTSTR sFilter = NULL);
    static BOOL  SaveFileDialog(HWND hWnd, LPTSTR sOutFilename, int nOutSize, LPCTSTR sFilter = NULL);
    static BOOL  BrowseFolder(LPTSTR sOutFoldername, int nOutSize, LPCTSTR sTitle = NULL);
    static ULONGLONG  GetDiskPartitionFreeSpaceEx(LPCTSTR lpDirectoryName);
    static void  GetPCTime(CString& strTime);
    static void  FormatTime(DWORD uTimeFrom, CString& strTimeTo);
    static void  ShowMenu(UINT* pnFlags, UINT_PTR* pnIDNewItem, TCHAR** lppNewItem, UINT nItemCount, CWnd* pParent);

    static unsigned long StrToUl(const char* sVal);
    static DOUBLE StrToDouble(const char* sVal);
    static DOUBLE CstrToDouble(CString str);

    static void    DoubleToStr(DOUBLE dbl, char* sBuff, int nBuffSiz);
    static void    DoubleToTstr(DOUBLE dbl, TCHAR* sBuff, int nBuffSiz);

    static void  CStringTocharstr(CString str, char* sBuff, int nBuffSiz);

    static void    CstrToTcharstr(CString str, TCHAR* sBuff, int nBuffSiz);

    static ULONG CStringToUlong(CString str);

    //十六进制字串转10进制数
    static ULONG    HexStrToUlong(const char*  sHexStr);

    static ULONG    HexNStrToUlong(const char* sHexStr, DWORD uStrSiz);

    static BOOL    HexNStrToHex(const char* sHexStr, DWORD uStrSiz, BYTE* Hex, DWORD& dwHexSiz);

    static ULONG    HexCstrToUlong(CString strHexStr);

    //10进制数转十六进制字串
    static char*    UlongToHexStr(ULONG uVal, /* OUT */char* sHexStr);

    //BCD字串转10进制数
    static ULONG    BcdStrToUlong(char* sBcdStr);

    //10进制数转BCD字串
    static char*    UlongToBcdStr(ULONG uVal, /* OUT */char* sBcdStr);

    //10进制数转BCD数
    static ULONG    DecimalToBcd(ULONG uVal);

    //BCD数转10进制数
    static ULONG    BcdToDecimal(ULONG uBcd);

    //BCD字串转BCD数
    static ULONG    BcdStrToBcd(char* sBcdStr);

    static void    BcdStrToBcdBuff(char* sBcdStr, ULONG uStrLength, BYTE* pBcdBuff, ULONG uBcdBuffSize);

    static BOOL    IsBcdStrValid(char* sBcdStr, ULONG uStrLength);

    static void    BcdToBcdStr(/*IN*/BYTE* pBcd, ULONG uBcdBytes, /*OUT*/char* pStr, ULONG uStrBytes);

    //十进制字串转10进制数
    static ULONG    DecimalStrToDecimal(char* sDecimalStr);

    static void    SwapString(char* str);
    static void    MakeDwordRange(DWORD& dwVal, DWORD dwMin, DWORD dwMax);

    static void    SetBit(DWORD* pVal, int bit, BOOL bValid);
    static BOOL    GetBit(DWORD dwVal, int bit);

    static void    SWAP_BYTEs(BYTE* pBytes, DWORD uCount);
    static WORD    SWAP_WORD(WORD word);
    static DWORD    SWAP_DWORD(DWORD dwDword);

    static WORD    MakeWord(BYTE byLowByte, BYTE byHiByte);

    static DWORD    MakeDword(WORD wLowWord, WORD wHiWord);

    static BOOL    IsNumStr(char* str);
    static BOOL    IsNumCstr(CString cstr);

    static void    NumCstrToProductCode(CString cstr, TagProductCode* pTagProductCode);

    static void    NumStrToBcdBytes(char* sNumStr, BYTE* pBYTEs, ULONG uSize);

    static void    ChangeIpDwordToBYTEs(DWORD dwIp, BYTE* pIp);

    //用于CDateTimeCtrl::SetFormat()
    static LPCTSTR    GetDateTimeCtrlFormatString();

    static UINT    GetEditTextUint(CEdit* pEd);
    static void    SetEditTextUint(CEdit* pEd, UINT uVal);

    static DOUBLE    GetEditTextDouble(CEdit* pEd);
    static void    SetEditTextDouble(CEdit* pEd, DOUBLE fVal);

    static DWORD    GetEditTextLength(CEdit* pEd);
    static BOOL    IsEditTextNumStr(CEdit* pEd);

    static BOOL    IsEditTextEmpty(CEdit* pEd);

    static void    GetEditTextStr(CEdit* pEd, char* pBuff, ULONG uSize);

    static void    T2C(LPCTSTR  wstr, LPSTR   cstr, int csiz);
    static void    Trace(void* pBuff, ULONG uSize);
};
#include "StdAfx.h"
#include <time.h>
#include <ShlObj.h>
#include "myutil.h"


myutil::myutil(void)
{
}

myutil::~myutil(void)
{
}

BOOL myutil::IsIntInRange(int value, int min, int max)
{
    return (min <= value) && (value <= max);
}

void myutil::SetIntInRange(int &value, int minval, int maxval)
{
    value = max(minval, value);
    value = min(maxval, value);
}

BOOL  myutil::IsUlongInRange(unsigned long value, unsigned long min, unsigned long max)
{
    return (min <= value) && (value <= max);
}

void myutil::SetIntInRange(unsigned long &value, unsigned long minval, unsigned long maxval)
{
    value = max(minval, value);
    value = min(maxval, value);
}

void myutil::IntTimeToSystemTime(unsigned long nTime, SYSTEMTIME &sTime)
{
    __time64_t nT = (__time64_t)nTime;
    //struct tm* p = _localtime64(&nT);
    struct tm tmo;
    struct tm* p = &tmo;
    _localtime64_s(&tmo, &nT);
    ::ZeroMemory(&sTime, sizeof(SYSTEMTIME));
    sTime.wYear = WORD(1900 + p->tm_year);
    sTime.wMonth = WORD(p->tm_mon);
    sTime.wDay = WORD(p->tm_mday);
    sTime.wHour = WORD(p->tm_hour);
    sTime.wMinute = WORD(p->tm_min);
    sTime.wSecond = WORD(p->tm_sec);
    sTime.wDayOfWeek = WORD(p->tm_wday);
}

void myutil::SystemTimeToIntTime(SYSTEMTIME sTime, unsigned long &nTime)
{
    struct tm t;
    ::ZeroMemory(&t, sizeof(struct tm));
    t.tm_year = sTime.wYear - 1900;
    t.tm_mon  = sTime.wMonth -1;
    t.tm_mday = sTime.wDay;
    t.tm_hour = sTime.wHour;
    t.tm_min  = sTime.wMinute;
    t.tm_sec  = sTime.wSecond;
    __time64_t nT = _mktime64(&t);

    nTime = (unsigned long)nT;
}

BOOL  myutil::OpenFileDialog(HWND hWnd, LPTSTR sOutFilename, int nOutSize, LPCTSTR sFilter/* = NULL*/)
{
    OPENFILENAME ofn;       // common dialog box structure
    //TCHAR szFile[MAX_PATH];       // buffer for file name
    ::ZeroMemory(sOutFilename, nOutSize);
    // Initialize OPENFILENAME
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = hWnd;
    ofn.lpstrFile = sOutFilename;
    //
    // Set lpstrFile[0] to ‘\0‘ so that GetOpenFileName does not 
    // use the contents of szFile to initialize itself.
    //
    ofn.lpstrFile[0] = \0;
    ofn.nMaxFile = nOutSize;
    if(sFilter) {
        ofn.lpstrFilter = sFilter;
    } else {
        ofn.lpstrFilter = TEXT("Image files (*.jpg;*.bmp)\0*.jpg;*.bmp\0\0");
    }
    ofn.nFilterIndex = 1;
    ofn.lpstrFileTitle = NULL;
    ofn.nMaxFileTitle = 0;
    ofn.lpstrInitialDir = NULL;
    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; 
    BOOL bRet =  GetOpenFileName((LPOPENFILENAME)&ofn);


    return bRet;
}

BOOL  myutil::SaveFileDialog(HWND hWnd, LPTSTR sOutFilename, int nOutSize, LPCTSTR sFilter/* = NULL*/)
{
    OPENFILENAME ofn;       // common dialog box structure
    //TCHAR szFile[MAX_PATH];       // buffer for file name
    ::ZeroMemory(sOutFilename, nOutSize);
    // Initialize OPENFILENAME
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = hWnd;
    ofn.lpstrFile = sOutFilename;
    //
    // Set lpstrFile[0] to ‘\0‘ so that GetOpenFileName does not 
    // use the contents of szFile to initialize itself.
    //
    ofn.lpstrFile[0] = \0;
    ofn.nMaxFile = nOutSize;
    if(sFilter) {
        ofn.lpstrFilter = sFilter;
    } else {
        ofn.lpstrFilter = TEXT("Image files (*.jpg;*.bmp)\0*.jpg;*.bmp\0\0");
    }
    ofn.nFilterIndex = 1;
    ofn.lpstrFileTitle = NULL;
    ofn.nMaxFileTitle = 0;
    ofn.lpstrInitialDir = NULL;
    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; 
    BOOL bRet = GetSaveFileName((LPOPENFILENAME)&ofn);


    return bRet;
}

BOOL myutil::BrowseFolder(LPTSTR sOutFoldername, int nOutSize, LPCTSTR sTitle /*= NULL*/)
{
    BOOL bRet = FALSE;
    LPMALLOC   pMalloc; 
    if   (::SHGetMalloc(&pMalloc)   ==   NOERROR) 
    { 
        BROWSEINFO   bi; 
        //TCHAR   pszBuffer[MAX_PATH]; 
        ::ZeroMemory(sOutFoldername, nOutSize);
        LPITEMIDLIST   pidl; 
        bi.hwndOwner   =   NULL; 
        bi.pidlRoot   =   NULL; 
        bi.pszDisplayName   =   sOutFoldername; 
        if(!sTitle) {
            bi.lpszTitle   =   _T( "Select a Directory"); 
        } else {
            bi.lpszTitle   =   sTitle; 
        }

        bi.ulFlags   =   BIF_RETURNFSANCESTORS   |   BIF_RETURNONLYFSDIRS; 
        bi.lpfn   =   NULL; 
        bi.lParam   =   0; 
        if   ((pidl   =   ::SHBrowseForFolder(&bi))   !=   NULL) 
        { 
            if   (::SHGetPathFromIDList(pidl,   sOutFoldername)) 
            {   
                //MessageBox(pszBuffer); 
                bRet = TRUE;
                
            } 
            pMalloc-> Free(pidl); 
        } 
        pMalloc-> Release();
    }

    return bRet;
}

ULONGLONG myutil::GetDiskPartitionFreeSpaceEx(LPCTSTR lpDirectoryName)
{
    ULARGE_INTEGER FreeBytesAvailableToCaller;
    ULARGE_INTEGER TotalNumberOfBytes;
    ULARGE_INTEGER TotalNumberOfFreeBytes;

    if(!GetDiskFreeSpaceEx(lpDirectoryName, &FreeBytesAvailableToCaller, &TotalNumberOfBytes, &TotalNumberOfFreeBytes))
    {
        return 0;
    }
    else
    {
        return TotalNumberOfFreeBytes.QuadPart;
    }
}

void  myutil::GetPCTime(CString& strTime)
{
    CTime t;
    __time64_t t64;
    time_t    tt;
    struct tm* ptm;
    struct tm tm0;
    char    sTime[64];
    CString tstr;

    t = CTime::GetCurrentTime();
    t64 = t.GetTime();
    tt = t64;

    //ptm = localtime(&tt);
    localtime_s(&tm0, &tt);
    ptm = &tm0;

    ZeroMemory(sTime, sizeof(sTime));
    sprintf_s(sTime, sizeof(sTime)-1, "%u-%02u-%02u %02u:%02u:%02u",
        ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday, 
        ptm->tm_hour, ptm->tm_min, ptm->tm_sec);

    strTime = sTime;
}

void  myutil::FormatTime(DWORD uTimeFrom, CString& strTimeTo)
{
    time_t t_;
    struct tm* ptm;
    struct tm tm0;
    char    sTime[64];

    t_ = uTimeFrom;
    //ptm = localtime(&t_);
    localtime_s(&tm0, &t_);
    ptm = &tm0;

    ZeroMemory(sTime, sizeof(sTime));
    sprintf_s(sTime, sizeof(sTime)-1, "%u-%02u-%02u %02u:%02u:%02u",
        ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday, 
        ptm->tm_hour, ptm->tm_min, ptm->tm_sec);

    strTimeTo = sTime;
}

/*
UINT uFlags[] = { MF_STRING, MF_STRING, MF_STRING };
UINT_PTR uItem[] = { IDCMD_RENAME, IDCMD_DELETE, IDCMD_DELETE_ALL };
TCHAR* sItem[] = { _T("重命名"), _T("删除"), _T("全部删除") };

myutil::ShowMenu(uFlags, uItem, sItem, 3, this);
*/
void  myutil::ShowMenu(UINT* pnFlags, UINT_PTR* pnIDNewItem, TCHAR** lppNewItem, UINT nItemCount, CWnd* pParent)
{
    int i;
    LPPOINT   lpoint=new   tagPOINT; 
    ::GetCursorPos(lpoint);//得到鼠标位置 
    CMenu   menu; 
    menu.CreatePopupMenu();//声明一个弹出式菜单 
    //增加菜单项“关闭”,点击则发送消息WM_DESTROY给主窗口(已 
    //隐藏),将程序结束。 
    //
    //menu.AppendMenu(MF_STRING,WM_DESTROY, _T("导出 "));  
    for (i=0; i<(int)nItemCount; ++i)
    {
        menu.AppendMenu((pnFlags[i]), pnIDNewItem[i], lppNewItem[i]);  
    }
    
    //确定弹出式菜单的位置 
    menu.TrackPopupMenu(TPM_LEFTALIGN,lpoint->x,lpoint->y,pParent); 
    //资源回收 
    HMENU   hmenu=menu.Detach(); 
    menu.DestroyMenu(); 
    delete   lpoint; 
}

unsigned long myutil::StrToUl(const char* sVal)
{
    return strtoul(sVal, NULL, 0);
}

DOUBLE myutil::StrToDouble(const char* sVal)
{
    return strtod(sVal, NULL);
}

DOUBLE myutil::CstrToDouble(CString str)
{
    char sBuff[MAX_PATH];

    ZeroMemory(sBuff, MAX_PATH);
    WideCharToMultiByte(CP_ACP,0,str.GetBuffer(),-1, sBuff, MAX_PATH-1,NULL,NULL);

    return strtod(sBuff, NULL);
}

void myutil::DoubleToStr(DOUBLE dbl, char* sBuff, int nBuffSiz)
{
    ZeroMemory(sBuff, nBuffSiz);
    sprintf_s(sBuff, nBuffSiz-1, "%f", dbl);
}

void myutil::DoubleToTstr(DOUBLE dbl, TCHAR* sBuff, int nBuffSiz)
{
    char sBuffer[MAX_PATH];

    ZeroMemory(sBuffer, MAX_PATH);
    ZeroMemory(sBuff, nBuffSiz*sizeof(TCHAR));

    sprintf_s(sBuffer, MAX_PATH-1, "%f", dbl);

    MultiByteToWideChar(CP_ACP,0,sBuffer,-1, sBuff, nBuffSiz-1);
}

void  myutil::CStringTocharstr(CString str, char* sBuff, int nBuffSiz)
{
    ZeroMemory(sBuff, nBuffSiz);
    int nLen = str.GetLength();
    WideCharToMultiByte(CP_ACP,0,str,-1,sBuff, nBuffSiz-1,NULL,NULL);
}

void myutil::CstrToTcharstr(CString str, TCHAR* sBuff, int nBuffSiz)
{
    ZeroMemory(sBuff, sizeof(TCHAR) * nBuffSiz);
    _tcscpy_s(sBuff, nBuffSiz, str.GetBuffer());
}

ULONG myutil::CStringToUlong(CString str)
{
    char    sBuff[MAX_PATH];

    ZeroMemory(sBuff, MAX_PATH);
    WideCharToMultiByte(CP_ACP,0,str.GetBuffer(),-1,sBuff,MAX_PATH-1,NULL,NULL);

    return StrToUl(sBuff);
}

ULONG    myutil::HexStrToUlong(const char* sHexStr)
{
    ULONG uVal, uTmp;

    if(!sHexStr)
        return 0;

    if(0 == strlen(sHexStr))
        return 0;
    uVal = 0;
    while(*sHexStr)
    {
        switch(*sHexStr)
        {
        case 0:case 1:case 2:case 3:case 4:
        case 5:case 6:case 7:case 8:case 9:
            uTmp = (ULONG)((*sHexStr) - 0);
            uVal = (uVal << 4);
            uVal = (uVal + uTmp);
            break;
        case A:case B:case C:case D:case E:
        case F:
            uTmp = (ULONG)((*sHexStr) - A + 10);
            uVal = (uVal << 4);
            uVal = (uVal + uTmp);
            break;
        case a:case b:case c:case d:case e:
        case f:
            uTmp = (ULONG)((*sHexStr) - a + 10);
            uVal = (uVal << 4);
            uVal = (uVal + uTmp);
            break;
        }

        sHexStr++;
    }

    return uVal;
}

ULONG    myutil::HexNStrToUlong(const char* sHexStr, DWORD uStrSiz)
{
    return 0;
}

BOOL myutil::HexNStrToHex(const char* sHexStr, DWORD uStrSiz, BYTE* Hex, DWORD& dwHexSiz)
{
    DWORD    iStr;
    DWORD    iHex;
    BYTE    byVal;
    BYTE    byTmp;

    ZeroMemory(Hex, dwHexSiz);

    iHex = 0;

    for (iStr=0; iStr<uStrSiz; iStr+= 2)
    {
        if(iHex >= dwHexSiz)
            break;

        byVal  = 0;

        switch(sHexStr[iStr])
        {
        case 0:case 1:case 2:case 3:case 4:
        case 5:case 6:case 7:case 8:case 9:
            byTmp = 0 + (BYTE)(sHexStr[iStr] - 0);
            break;
        case A:case B:case C:case D:case E:case F:
            byTmp = 10 + (BYTE)(sHexStr[iStr] - A);
            break;
        case a:case b:case c:case d:case e:case f:
            byTmp = 10 + (BYTE)(sHexStr[iStr] - a);
            break;
        default:
            return FALSE;
            break;
        }
        byTmp = (byTmp << 4);
        byVal = byTmp;

        if((iStr+1) >= uStrSiz)
            break;

        switch(sHexStr[iStr+1])
        {
        case 0:case 1:case 2:case 3:case 4:
        case 5:case 6:case 7:case 8:case 9:
            byTmp = 0 + (BYTE)(sHexStr[iStr+1] - 0);
            break;
        case A:case B:case C:case D:case E:case F:
            byTmp = 10 + (BYTE)(sHexStr[iStr+1] - A);
            break;
        case a:case b:case c:case d:case e:case f:
            byTmp = 10 + (BYTE)(sHexStr[iStr+1] - a);
            break;
        default:
            return FALSE;
            break;
        }
        byVal += byTmp;

        Hex[iHex] = byVal;
        ++iHex;
    }

    dwHexSiz = iHex;
    return TRUE;
}

ULONG myutil::HexCstrToUlong(CString strHexStr)
{
    char    sBuff[MAX_PATH];

    ZeroMemory(sBuff, MAX_PATH);
    WideCharToMultiByte(CP_ACP,0,strHexStr.GetBuffer(),-1,sBuff,MAX_PATH-1,NULL,NULL);

    return HexStrToUlong(sBuff);
}

char*    myutil::UlongToHexStr(ULONG uVal, /* OUT */char* sHexStr)
{
    ULONG uTmp;
    int pos;

    char*    sHexNum = "0123456789ABCDEF";

    sHexStr[0] = 0;
    sHexStr[1] = \0;

    pos = 0;
    while(uVal > 0)
    {
        uTmp = (uVal & 0xF);
        uVal = (uVal >> 4);
        sHexStr[pos] = sHexNum[uTmp];
        sHexStr[pos+1] = \0;
    }

    SwapString(sHexStr);
    return sHexStr;
}

ULONG    myutil::BcdStrToUlong(char* sBcdStr)
{
    ULONG uVal, uTmp;

    if(!sBcdStr)
        return 0;

    if(0 == strlen(sBcdStr))
        return 0;
    uVal = 0;
    while(*sBcdStr)
    {
        switch(*sBcdStr)
        {
        case 0:case 1:case 2:case 3:case 4:
        case 5:case 6:case 7:case 8:case 9:
            uTmp = (ULONG)((*sBcdStr) - 0);
            uVal = (uVal * 10);
            uVal = (uVal + uTmp);
            break;
        //case ‘A‘:case ‘B‘:case ‘C‘:case ‘D‘:case ‘E‘:
        //case ‘F‘: /* BCD码不会有 ABCDEF */
        //    uTmp = (ULONG)((*sBcdStr) - ‘A‘ + 10);
        //    uVal = (uVal * 10);
        //    uVal = (uVal + uTmp);
        //    break;
        //case ‘a‘:case ‘b‘:case ‘c‘:case ‘d‘:case ‘e‘:
        //case ‘f‘:
        //    uTmp = (ULONG)((*sBcdStr) - ‘a‘ + 10);
        //    uVal = (uVal * 10);
        //    uVal = (uVal + uTmp);
        //    break;
        }

        sBcdStr++;
    }

    return uVal;
}

char*    myutil::UlongToBcdStr(ULONG uVal, /* OUT */char* sBcdStr)
{
    ULONG uTmp;
    int pos;

    char*    sHexNum = "0123456789";

    sBcdStr[0] = 0;
    sBcdStr[1] = \0;

    pos = 0;
    while(uVal > 0)
    {
        uTmp = (uVal % 10);
        uVal = (uVal / 10);
        sBcdStr[pos] = sHexNum[uTmp];
        sBcdStr[pos+1] = \0;
    }

    SwapString(sBcdStr);
    return sBcdStr;
}

ULONG myutil::DecimalToBcd(ULONG uVal)
{
    ULONG uTmpTmp;
    ULONG uTmpVal;
    ULONG uMultiply;

    uTmpTmp = 0;
    uTmpVal = 0;
    uMultiply = 1;

    while(uVal > 0)
    {
        uTmpTmp = uVal % 10;
        uVal = (uVal / 10);

        uTmpTmp = uTmpTmp * uMultiply;
        uTmpVal += uTmpTmp;

        uMultiply *= 16;
    }

    return uTmpVal;
}

ULONG    myutil::BcdToDecimal(ULONG uBcd)
{
    ULONG uTmpTmp;
    ULONG uTmpVal;
    ULONG uMultiply;

    uTmpTmp = 0;
    uTmpVal = 0;
    uMultiply = 1;

    while(uBcd > 0)
    {
        uTmpTmp = (uBcd & 0xF);
        uTmpTmp *= uMultiply;
        uBcd = (uBcd >> 4);
        uMultiply *= 10;

        uTmpVal += uTmpTmp;
    }

    return uTmpVal;
}

ULONG    myutil::BcdStrToBcd(char* sBcdStr)
{
    ULONG uVal, uTmp;

    if(!sBcdStr)
        return 0;

    if(0 == strlen(sBcdStr))
        return 0;
    uVal = 0;
    while(*sBcdStr)
    {
        switch(*sBcdStr)
        {
        case 0:case 1:case 2:case 3:case 4:
        case 5:case 6:case 7:case 8:case 9:
            uTmp = (ULONG)((*sBcdStr) - 0);
            uVal = (uVal << 4);
            uVal = (uVal + uTmp);
            break;
            //case ‘A‘:case ‘B‘:case ‘C‘:case ‘D‘:case ‘E‘:
            //case ‘F‘: /* BCD码不会有 ABCDEF */
            //    uTmp = (ULONG)((*sBcdStr) - ‘A‘ + 10);
            //    uVal = (uVal * 10);
            //    uVal = (uVal + uTmp);
            //    break;
            //case ‘a‘:case ‘b‘:case ‘c‘:case ‘d‘:case ‘e‘:
            //case ‘f‘:
            //    uTmp = (ULONG)((*sBcdStr) - ‘a‘ + 10);
            //    uVal = (uVal * 10);
            //    uVal = (uVal + uTmp);
            //    break;
        }

        sBcdStr++;
    }

    return uVal;
}

void    myutil::BcdStrToBcdBuff(char* sBcdStr, ULONG uStrLength, BYTE* pBcdBuff, ULONG uBcdBuffSize)
{
    int i;
    BYTE byTmp;
    BYTE byVal;

    ULONG uPos;

    ZeroMemory(pBcdBuff, uBcdBuffSize);
    uPos = 0;
    for (i=0; i<uStrLength; i+=2)
    {
        byTmp = 0;
        byVal = 0;
        if((sBcdStr[i] >= 0) && (sBcdStr[i] <= 9))
        {
            byTmp = (BYTE)(sBcdStr[i] - 0);
            byTmp = (byTmp << 4);
            byVal = byTmp;
        }

        if((i+1) >= uStrLength)
            break;

        if((sBcdStr[i+1] >= 0) && (sBcdStr[i+1] <= 9))
        {
            byTmp = (BYTE)(sBcdStr[i+1] - 0);
            byVal += byTmp;
        }

        if(uPos < uBcdBuffSize) {
            pBcdBuff[uPos] = byVal;
            ++uPos;
        }
    }
}

BOOL    myutil::IsBcdStrValid(char* sBcdStr, ULONG uStrLength)
{
    BOOL bResult;
    int i;

    bResult = TRUE;

    for (i=0; i<uStrLength; ++i)
    {
        if((sBcdStr[i] >= 0) && (sBcdStr[i] <= 9))
        {
            
        }
        else
        {
            bResult = FALSE;
            break;
        }
    }
    return bResult;
}

void    myutil::BcdToBcdStr(/*IN*/BYTE* pBcd, ULONG uBcdBytes, /*OUT*/char* pStr, ULONG uStrBytes)
{
    ULONG uPos, uBcdPos;
    BYTE  byIdx;

    char* pNum = "01234567899999999999";

    ZeroMemory(pStr, uStrBytes);

    uPos = 0;
    uBcdPos = 0;
    while((uPos+1)<uStrBytes)
    {
        if(uBcdPos>=uBcdBytes)
            break;

        byIdx = ((pBcd[uBcdPos] & 0xF0) >> 4);
        pStr[uPos] = pNum[byIdx];
        ++uPos;

        byIdx = (pBcd[uBcdPos] & 0xF);
        pStr[uPos] = pNum[byIdx];
        ++uPos;



        ++uBcdPos;
    }
}

ULONG    myutil::DecimalStrToDecimal(char* sDecimalStr)
{
    ULONG uVal, uTmp;

    if(!sDecimalStr)
        return 0;

    if(0 == strlen(sDecimalStr))
        return 0;
    uVal = 0;
    while(*sDecimalStr)
    {
        switch(*sDecimalStr)
        {
        case 0:case 1:case 2:case 3:case 4:
        case 5:case 6:case 7:case 8:case 9:
            uTmp = (ULONG)((*sDecimalStr) - 0);
            uVal = (uVal * 10);
            uVal = (uVal + uTmp);
            break;
        }

        sDecimalStr++;
    }

    return uVal;
}

void    myutil::SwapString(char* str)
{
    int nLength;
    int i;
    char    tp;
    if(!str)
        return;
    if(strlen(str) < 2)
        return;

    nLength = (int)strlen(str);
    for (i=0; i<nLength/2; ++i)
    {
        tp = str[i];
        str[i] = str[nLength-i-1];
        str[nLength-i-1] = tp;
    }
}

void  myutil::MakeDwordRange(DWORD& dwVal, DWORD dwMin, DWORD dwMax)
{
    dwVal = (dwVal < dwMin) ? dwMin : dwVal;
    dwVal = (dwVal > dwMax) ? dwMax : dwVal;
}

void myutil::SetBit(DWORD* pVal, int bit, BOOL bValid)
{
    DWORD dw;

    if(bValid) {
        dw = 1;
        dw = (dw << bit);
        dw = (dw | (*pVal));
        (*pVal) = dw;
    } else {
        dw = 1;
        dw = (dw << bit);
        dw = (~dw);
        dw = (dw & (*pVal));
        (*pVal) = dw;
    }
}

BOOL myutil::GetBit(DWORD dwVal, int bit)
{
    DWORD dw;

    if(bit<0)
        return FALSE;
    if(bit>32)
        return FALSE;

    dw = 1;
    dw = (dw << bit);
    dw = (dw & dwVal);

    return (dw != 0);
}

void myutil::SWAP_BYTEs(BYTE* pBytes, DWORD uCount)
{
    DWORD i, ii;
    BYTE  tmp;

    for (i=0, ii=uCount-1; i<ii; ++i, --ii)
    {
        tmp = pBytes[i];
        pBytes[i]  = pBytes[ii];
        pBytes[ii] = tmp;
    }
}

WORD myutil::SWAP_WORD(WORD word)
{
    BYTE tmp;
    BYTE* b;

    WORD w;

    w = word;
    b = (BYTE*)&w;
    tmp = b[0];
    b[0] = b[1];
    b[1]  = tmp;

    return w;
}

DWORD myutil::SWAP_DWORD(DWORD dwDword)
{
    DWORD dwVal;
    BYTE    tmp;
    BYTE* b;

    dwVal = dwDword;
    b = (BYTE*)(&dwVal);

    tmp = b[0];
    b[0] = b[3];
    b[3] = tmp;

    tmp = b[1];
    b[1] = b[2];
    b[2] = tmp;

    return dwVal;
}

WORD myutil::MakeWord(BYTE byLowByte, BYTE byHiByte)
{
    WORD wVal;

    wVal = (WORD)byHiByte;
    wVal = (wVal << 8);

    wVal += (WORD)byLowByte;

    return wVal;
}

DWORD myutil::MakeDword(WORD wLowWord, WORD wHiWord)
{
    DWORD    dwVal;

    dwVal = wHiWord;
    dwVal = (dwVal << 16);

    dwVal += wLowWord;

    return dwVal;
}

BOOL myutil::IsNumStr(char* str)
{
    int nLen;
    int i;
    const char* pNum = "0123456789";

    nLen = strlen(str);

    for (i=0; i<nLen; ++i)
    {
        if(str[i] < 0)
            return FALSE;
        if(str[i] > 9)
            return FALSE;
    }

    return TRUE;
}

BOOL myutil::IsNumCstr(CString cstr)
{
    char    sBuff[MAX_PATH];
    ZeroMemory(sBuff, MAX_PATH);
    int nLen = cstr.GetLength();
    WideCharToMultiByte(CP_ACP,0, cstr,-1,sBuff, MAX_PATH-1,NULL,NULL);

    return IsNumStr(sBuff);
}

void myutil::NumCstrToProductCode(CString cstr, TagProductCode* pTagProductCode)
{
    char    sBuff[MAX_PATH];
    ZeroMemory(sBuff, MAX_PATH);
    int nLen = cstr.GetLength();
    WideCharToMultiByte(CP_ACP,0, cstr,-1,sBuff, MAX_PATH-1,NULL,NULL);

    int i, a;
    BYTE byVal;
    BYTE byTmp;

    ZeroMemory(pTagProductCode, sizeof(TagProductCode));
    for (i=0; i<12; i += 2)
    {
        byVal = 0;
        byTmp = (BYTE)(sBuff[i] - 0);
        byTmp = (byTmp << 4);
        byVal = byTmp;

        byTmp = (BYTE)(sBuff[i+1] - 0);
        byVal += byTmp;

        a = i/2;
        a = 5 - a;

        pTagProductCode->A[a] = byVal;
    }
}

void myutil::NumStrToBcdBytes(char* sNumStr, BYTE* pBYTEs, ULONG uSize)
{
    int i, iPos;
    int nLen;
    BYTE cBYTE;

    ZeroMemory(pBYTEs, uSize);
    nLen = strlen(sNumStr);

    i=0;
    iPos = 0;
    for (; ; )
    {
        if(i >= nLen)
            break;
        if(iPos >= uSize)
            break;
        cBYTE = (BYTE)(sNumStr[i]-0);
        cBYTE = (cBYTE & 0xF);
        pBYTEs[iPos] = (cBYTE<<4);
        ++i;

        if(i >= nLen)
            break;
        if(iPos >= uSize)
            break;
        cBYTE = (BYTE)(sNumStr[i]-0);
        cBYTE = (cBYTE & 0xF);
        pBYTEs[iPos] += cBYTE;
        ++i;

        ++iPos;
    }
}

void myutil::ChangeIpDwordToBYTEs(DWORD dwIp, BYTE* pIp)
{
    BYTE* p = (BYTE*)&dwIp;

    pIp[0] = p[1];
    pIp[1] = p[0];
    pIp[2] = p[3];
    pIp[3] = p[2];
}

LPCTSTR myutil::GetDateTimeCtrlFormatString()
{
    static TCHAR* s_str = _T("yyyy-MM-dd HH:mm:ss");

    return s_str;
}

UINT myutil::GetEditTextUint(CEdit* pEd)
{
    UINT uVal;
    CString str;

    pEd->GetWindowText(str);
    return CStringToUlong(str);
}

void myutil::SetEditTextUint(CEdit* pEd, UINT uVal)
{
    CString str;

    str.Format(_T("%u"), uVal);

    pEd->SetWindowText(str);
}

DOUBLE myutil::GetEditTextDouble(CEdit* pEd)
{
    char sBuff[MAX_PATH];
    CString str;

    pEd->GetWindowText(str);

    ZeroMemory(sBuff, MAX_PATH);
    WideCharToMultiByte(CP_ACP,0,str.GetBuffer(),-1, sBuff, MAX_PATH-1,NULL,NULL);

    return strtod(sBuff, NULL);
}

void myutil::SetEditTextDouble(CEdit* pEd, DOUBLE fVal)
{
    CString str;
    char sBuff[MAX_PATH];

    ZeroMemory(sBuff, MAX_PATH);
    sprintf_s(sBuff, MAX_PATH-1, "%f", fVal);
    str = sBuff;

    pEd->SetWindowText(str);
}

DWORD myutil::GetEditTextLength(CEdit* pEd)
{
    CString str;
    DWORD dwLength;
    int    nLength;
    pEd->GetWindowText(str);

    nLength = str.GetLength();
    if(nLength<0)
        nLength = 0;
    dwLength = (DWORD)nLength;
    return dwLength;
}

BOOL myutil::IsEditTextNumStr(CEdit* pEd)
{
    CString str;
    pEd->GetWindowText(str);

    return IsNumCstr(str);
}

BOOL myutil::IsEditTextEmpty(CEdit* pEd)
{
    CString str;
    pEd->GetWindowText(str);

    return str.IsEmpty();    
}

void myutil::GetEditTextStr(CEdit* pEd, char* pBuff, ULONG uSize)
{
    CString str;
    char    sBuff[MAX_PATH];

    ZeroMemory(sBuff, MAX_PATH);
    pEd->GetWindowText(str);

    WideCharToMultiByte(CP_ACP,0,str.GetBuffer(),-1,pBuff,uSize,NULL,NULL);
}

void myutil::T2C(LPCTSTR  wstr, LPSTR   cstr, int csiz)
{
    ZeroMemory(cstr, csiz);
    WideCharToMultiByte(CP_ACP,0,wstr,-1,cstr,csiz,NULL,NULL);
}

void myutil::Trace(void* pBuff, ULONG uSize)
{
    int i;
    BYTE* pBuf;

    pBuf = (BYTE*)pBuff;
    for (i=0; i<uSize; ++i)
    {
        TRACE(_T("%02X "), pBuf[i]);
    }
    TRACE(_T("\r\n"));
}

 

以上是关于myutil 20170602的主要内容,如果未能解决你的问题,请参考以下文章

大数据:一周电影榜20170602

OpenGL第二节:绘制多个颜色四边形

opencv项目实战——信用卡数字识别

关于备份PFILE

实验报告二 面向对象程序设计

微信小程序代码片段