// cv.cpp: implementation of the cv class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "machine_365e.h"
#include "cv.h"
#include <locale.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
cv::cv()
{
}
cv::~cv()
{
}
int cv::cstring_to_int(CString t)
{
return string_to_int(cstring_to_string(t));
}
string cv::cstring_to_string(CString text)
{
if (text.IsEmpty()) return "";
string str_result = "";
char* buffer = new char[text.GetLength() * 2 + 2];
memset(buffer, 0, text.GetLength() * 2 + 2);
#if defined(_M_IX86)
WideCharToMultiByte(CP_ACP, 0, text, text.GetLength(),
buffer, text.GetLength() * 2 + 2, NULL, NULL);
#else
WideCharToMultiByte(/* CP_ACP */ 936, 0, text, text.GetLength(),
buffer, text.GetLength() * 2 + 2, NULL, NULL);
#endif
str_result = buffer;
delete[] buffer;
return str_result;
}
//gb2312?Unicode
void cv::Gb2312ToUnicode(wchar_t* pstrOut, u32 dwOutLen, const char* pstrIn, u32 dwInLen)
{
if (NULL == pstrOut)
{
return ;
}
#ifdef WIN32
u32 i = MultiByteToWideChar(CP_ACP, 0, pstrIn, -1, NULL, 0);
if (i >= dwOutLen)
{
i = dwOutLen - 1;
}
MultiByteToWideChar(CP_ACP, 0, pstrIn, -1, pstrOut, i);
#else
iconv_t iConv = iconv_open("unicode", "gb2312");
iconv(iConv, (char**)pstrIn, &dwInLen, (char**)pstrOut, &dwOutLen);
iconv_close(iConv);
#endif
}
//gb2312?utf8
void cv::Gb2312ToUtf8(char* pstrOut, u32 dwOutLen, const char* pstrIn, u32 dwInLen)
{
#ifdef WIN32
int i = MultiByteToWideChar(CP_ACP, 0, pstrIn, -1, NULL, 0);
wchar_t * strSrc = new wchar_t[i+1];
MultiByteToWideChar(CP_ACP, 0, pstrIn, -1, strSrc, i);
i = WideCharToMultiByte(CP_UTF8, 0, strSrc, -1, NULL, 0, NULL, NULL);
if (i >= dwOutLen)
{
i = dwOutLen - 1;
}
WideCharToMultiByte(CP_UTF8, 0, strSrc, -1, pstrOut, i, NULL, NULL);
delete strSrc;
#else
iconv_t iConv = iconv_open("utf-8", "gb2312");
iconv(iConv, (char **)&pstrIn, (size_t *)&dwInLen, (char **)&pstrOut, (size_t *)&dwOutLen);
iconv_close(iConv);
#endif
}
BOOL cv::IsNumeric(string text)
{
for (int i = 0; i < text.length(); i++)
{
if (!(text[i] >= '0' && text[i] <= '9'))
return FALSE;
}
return TRUE;
}
CString cv::string_to_cstring(string text)
{
if (text.empty()) return CString("");
WCHAR* buffer = new WCHAR[text.length() * 2 + 2];
memset(buffer, 0, sizeof(WCHAR) * 2 + 2);
#if defined(_M_IX86)
MultiByteToWideChar(CP_ACP, 0, text.c_str(),
-1, buffer, text.length() * 2 + 2);
#else
MultiByteToWideChar(/* CP_ACP */ 936, 0, text.c_str(),
-1, buffer, text.length() * 2 + 2);
#endif
CString str_result = _T("");
str_result = buffer;
delete buffer;
return str_result;
}
int cv::string_to_int(string str)
{
return atoi(str.c_str());
}
//Unicode?Gb2312
void cv::UnicodeToGb2312(char* pstrOut, u32 dwOutLen, const wchar_t* pstrIn, u32 dwInLen)
{
if (NULL == pstrOut)
{
return ;
}
#ifdef WIN32
u32 i = WideCharToMultiByte(CP_ACP, 0, pstrIn, -1, NULL, 0, NULL, NULL);
if (i >= dwOutLen)
{
i = dwOutLen - 1;
}
WideCharToMultiByte(CP_ACP, 0, pstrIn, -1, pstrOut, i, 0, 0);
#else
iconv_t iConv = iconv_open("gb2312", "unicode");
iconv(iConv, (char**)pstrIn, &dwInLen, (char**)pstrOut, &dwOutLen);
iconv_close(iConv);
#endif
}
//utf8?gb2312
void cv::Utf8ToGb2312(char* pstrOut, u32 dwOutLen, const char* pstrIn, u32 dwInLen)
{
if (NULL == pstrOut)
{
return ;
}
#ifdef WIN32
int i = MultiByteToWideChar(CP_UTF8, 0, pstrIn, -1, NULL, 0);
wchar_t * strSrc = new wchar_t[i+1];
MultiByteToWideChar(CP_UTF8, 0, pstrIn, -1, strSrc, i);
i = WideCharToMultiByte(CP_ACP, 0, strSrc, -1, NULL, 0, NULL, NULL);
if (i >= dwOutLen)
{
i = dwOutLen - 1;
}
WideCharToMultiByte(CP_ACP, 0, strSrc, -1, pstrOut, i, NULL, NULL);
delete strSrc;
#else
iconv_t iConv = iconv_open("gb2312", "utf-8");
iconv(iConv, (char **)&pstrIn, (size_t *)&dwInLen, (char **)&pstrOut, (size_t *)&dwOutLen);
iconv_close(iConv);
#endif
}
//utf8?Unicode
void cv::Utf8ToUnicode(wchar_t* pstrOut, u32 dwOutLen, const char* pstrIn, u32 dwInLen)
{
if (NULL == pstrOut)
{
return ;
}
#ifdef WIN32
u32 i = MultiByteToWideChar(CP_UTF8, 0, pstrIn, -1, NULL, 0);
if (i >= dwOutLen)
{
i = dwOutLen - 1;
}
MultiByteToWideChar(CP_UTF8, 0, pstrIn, -1, pstrOut, i);
#else
iconv_t iConv = iconv_open("unicode", "utf-8");
iconv(iConv, (char**)pstrIn, &dwInLen, (char**)pstrOut, &dwOutLen);
iconv_close(iConv);
#endif
}
int cv::cs2i(CString s)
{
int i;
i= cv::cstring_to_int(s);
return i;
}
string cv::cs2s(CString s)
{
return cv::cstring_to_string(s);
}
CString cv::i2cs(int i)
{
CString s;
s.Format(L"%d",i);
return s;
}
CString cv::s2cs(string text)
{
return cv::string_to_cstring(text);
}
string cv::int_to_string(int n)
{
char buffer[32] = { 0 };
sprintf(buffer, "%d", n);
return string(buffer);
}
//--------------------转换汉字部分 begin-----------------------
CString cv::ChineseCapitalMoney(double Num) {
CString szChMoney,szNum;
int iLen, iNum, iAddZero=0;
TCHAR* hzUnit[18]={_T("分"),_T("角"),_T("元"),_T("拾"),_T("佰"),_T("仟"),_T("万"),_T("拾"),_T("佰"),_T("仟"),_T("亿"),_T("拾"),_T("佰"),_T("仟"),_T("万"),_T("拾"),_T("佰"),_T("仟")};
TCHAR* hzNum[10]={_T("零"),_T("壹"),_T("贰"),_T("叁"),_T("肆"),_T("伍"),_T("陆"),_T("柒"),_T("捌"),_T("玖")};
if (-0.01<Num && Num<0.01) return _T("零元整");
szNum.Format(_T("%18.0f"), Num*100); // 最小到分
szNum.TrimLeft();
iLen=szNum.GetLength();
if(iLen>15 || iLen==0 || Num<0) return _T(""); // 数据错误返回
for(int i=0;i<iLen;i++) {
iNum=_ttoi((LPCTSTR)szNum.Mid(i,1));
if (iNum==0) {
iAddZero++;
} else {
if (iAddZero>0) szChMoney+=_T("零");
szChMoney+=hzNum[iNum];
iAddZero=0;
}
if (iNum!=0 || iLen-i==3 || iLen-i==11 || ((iLen-i+1)%8==0 && iAddZero<4)) szChMoney+=hzUnit[iLen-i-1];
}
if (szNum.Right(2)==_T("00")) szChMoney+=_T("整"); // 没有角和分
return szChMoney;
}
//--------------------转换汉字部分 end-----------------------
CString cv::charx2cs(char *s)
{
CString cs;
string ss;
ss.assign(s);
//sprintf(s,"select * 中国from %s",tbns.c_str());
cs=::cv::s2cs(ss);//这个好用,测试对汉语支持也是正确的! 就是说用sprintf载入的字符是对的,奇怪的是哪儿标定了unicode?
//cs.Format(L"%s",s);//这个不能用,用后unicode出乱码,
return cs;
}
//DEL CString cv::ws2cs(wstring ws)
//DEL {
//DEL string s;
//DEL s=ws2s(ws);
//DEL return s2cs(s);
//DEL
//DEL
//DEL
//DEL }
//c的locu的不好用
//---------------hy try2012-9-10---------------
std::string cv::WChar2Ansi(LPCWSTR pwszSrc)
{
int nLen = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL);
if (nLen<= 0) return std::string("");
char* pszDst = new char[nLen];
if (NULL == pszDst) return std::string("");
WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pszDst, nLen, NULL, NULL);
pszDst[nLen -1] = 0;
std::string strTemp(pszDst);
delete [] pszDst;
return strTemp;
}
string cv::ws2s(wstring& inputws){ return WChar2Ansi(inputws.c_str()); }
//Converting a Ansi string to WChar string
std::wstring cv::Ansi2WChar(LPCSTR pszSrc, int nLen)
{
int nSize = Mul