You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
162 lines
3.1 KiB
C
162 lines
3.1 KiB
C
#pragma once
|
|
#include "GlobalDefine.h"
|
|
#include "BitOperation.h"
|
|
#define TRUE_STR _T("TRUE")
|
|
#define FALSE_STR _T("FALSE")
|
|
//和MFC CString 相关的一些函数
|
|
|
|
//去掉小数点后面不要的0
|
|
inline void DeleteZero(CString &s)
|
|
{
|
|
int nIndex;
|
|
nIndex=s.Find('.');
|
|
if (nIndex>=0)
|
|
{
|
|
s.TrimRight('0');
|
|
if (s.GetLength()==nIndex+1)
|
|
{
|
|
s=s.Left(nIndex);
|
|
if (s.IsEmpty())
|
|
s='0';
|
|
}
|
|
}
|
|
}
|
|
|
|
//double 转换为CString (DigitsCnt 是小数点后保留位数,最多== 6)
|
|
inline CString Db2CString(double n,int DigitsCnt = -1)
|
|
{
|
|
CString s;
|
|
if(DigitsCnt==0)
|
|
s.Format("%ld",(int)n);
|
|
if(DigitsCnt==1)
|
|
s.Format("%.1f",n);
|
|
if(DigitsCnt==2)
|
|
s.Format("%.02f",n);
|
|
if(DigitsCnt==3)
|
|
s.Format("%.03f",n);
|
|
if(DigitsCnt==4)
|
|
s.Format("%.04f",n);
|
|
if(DigitsCnt==5)
|
|
s.Format("%.05f",n);
|
|
if(DigitsCnt==6)
|
|
s.Format("%.06f",n);
|
|
if(DigitsCnt<0)
|
|
{
|
|
s.Format("%lf",n);
|
|
//删除末尾的0
|
|
DeleteZero(s);
|
|
}
|
|
return s;
|
|
}
|
|
//int 转换为CString
|
|
inline CString Int2CString(int n)
|
|
{
|
|
CString s;
|
|
s.Format("%ld",n);
|
|
return s;
|
|
}
|
|
inline CString ByteToString(BYTE bit)
|
|
{
|
|
CString str;
|
|
for(int i=7;i>=0;i--)
|
|
{
|
|
if(IsBitOn(bit,i))
|
|
str += "1";
|
|
else
|
|
str += "0";
|
|
}
|
|
return str;
|
|
}
|
|
//32位int
|
|
inline CString IntByteToString(int bit)
|
|
{
|
|
CString str;
|
|
for(int i=31;i>=0;i--)
|
|
{
|
|
if(IsBitOn(bit,i))
|
|
str += "1";
|
|
else
|
|
str += "0";
|
|
}
|
|
return str;
|
|
}
|
|
//获取bool 类型的str
|
|
inline CString GetBoolValStr(bool b)
|
|
{
|
|
if(b)
|
|
return TRUE_STR;
|
|
else
|
|
return FALSE_STR;
|
|
}
|
|
//在str 中查找SubStr ,找到返回true
|
|
inline bool FindSubStr(CString &str,CString &SubStr)
|
|
{
|
|
return (str.Find(SubStr) != -1);
|
|
}
|
|
//删除子串,并返回剩余右边的部分
|
|
inline CString DelSubStr(CString str,CString &SubStr)
|
|
{
|
|
int pos = str.Find(SubStr);
|
|
if(pos != -1)
|
|
{
|
|
str = str.Right(str.GetLength()-(pos+SubStr.GetLength()));
|
|
}
|
|
return str;
|
|
}
|
|
inline int CStringToInt(CString &str)
|
|
{
|
|
return _tstoi(LPCTSTR(str));
|
|
}
|
|
inline double CStringToDouble(CString &str)
|
|
{
|
|
return _tstof(LPCTSTR(str));
|
|
}
|
|
inline CString Bool2Str(bool b)
|
|
{
|
|
if(b)
|
|
return "TRUE";
|
|
else
|
|
return "FALSE";
|
|
}
|
|
inline CString Bool2CString(bool b)
|
|
{
|
|
if(b)
|
|
return "1";
|
|
else
|
|
return "0";
|
|
}
|
|
inline CString Bool2CStringOnOff(bool b)
|
|
{
|
|
if(b)
|
|
return "On";
|
|
else
|
|
return "Off";
|
|
}
|
|
inline CString Bool2CStringYesNo(bool b)
|
|
{
|
|
if(b)
|
|
return "Yes";
|
|
else
|
|
return "No";
|
|
}
|
|
inline bool CStringToBool(CString &str)
|
|
{
|
|
if(str == ("TRUE")||str == ("Yes"))
|
|
return true;
|
|
else
|
|
return false;
|
|
}
|
|
inline CString Int2Str_LeftZero(int n)
|
|
{
|
|
CString s;
|
|
if(n<10)
|
|
s.Format("0%ld",n);//左边补0
|
|
else
|
|
s.Format("%ld",n);
|
|
return s;
|
|
}
|
|
//判断String 是否为数字
|
|
inline bool IsStringDigit(CString s)
|
|
{
|
|
return (s.SpanIncluding("0123456789") == s);
|
|
} |