2020-11-19

1.unicode to ansi
2 ansi to unicode
3.unicode to hex string
4. ansicode to hex string

void CRead_Ethenet_ComDlg::OnBnClickedButton1()
{
CString strTemp = _T(“12AB我们\0”) ;
CString strDestData, strBak = strTemp ;

int	nSize = strTemp.GetLength() ;
Trans_UnicodeToHexStr( strTemp, strDestData ) ;

m_strEditMsg +=_T("\r\n UNCODE ") ;
m_strEditMsg += strTemp ;
m_strEditMsg +=_T("\r\n HEX ") ;

m_strEditMsg += strDestData ;

UnicodeToAnsiSimple( strTemp );
//::AfxMessageBox( strTemp ) ;
//strTemp.ReleaseBuffer() ;

m_strEditMsg +=_T("\r\n ANSI ") ;
m_strEditMsg += strTemp ;

strDestData = _T("");
strDestData.ReleaseBuffer() ;
Trans_AnsiToHexStr( strTemp, strDestData, nSize) ;
m_strEditMsg +=_T("\r\n HEX ") ;
m_strEditMsg += strDestData ;

strDestData = _T("");
strDestData.ReleaseBuffer() ;
trans_Ansi_To_Unicode(strTemp, strDestData);
strTemp = strDestData ;
strTemp.ReleaseBuffer() ;

m_strEditMsg +=_T("\r\n UNCODE ") ;
m_strEditMsg += strTemp ;

strDestData = _T("");
strDestData.ReleaseBuffer() ;
Trans_UnicodeToHexStr( strTemp, strDestData ) ;
m_strEditMsg +=_T("\r\n HEX ") ;
m_strEditMsg += strDestData ;

UpdateData( false ) ;

}

/ CommFunction.h
#pragma once

#include “stdafx.h”
//#include “afxdialogex.h”

void SetCurrentDirectorySimple();

void UnicodeToAnsiSimple(CString &strData);

void trans_Unicode_To_Ansi(CString &strData, CString &strDest);
void trans_Ansi_To_Unicode(CString &strData, CString &strDest);

void ClearClipboardContent();
void WriteToClipboard(CString m_editRead);
void AddToClipboard(CString m_editRead);
char * ReadFromClipboard();

void trans_Multicode_To_Ansi(CString &strData, CString &strDest);

BOOL MByteToWChar(LPCSTR lpcszStr, LPWSTR lpwszStr, DWORD dwSize) ;

CString Convert_DoubleToString( double dValue) ;

static CString str_FileName = _T(“D:\T.TXT”) ;
extern CString m_strFileMsg ;
static CFile *p_FileHandle = NULL, h_FileHandle ;
extern CStdioFile h_StdioFile, *pHandle_StdioFile ;

bool Open_File( CString strFileName, DWORD& dwFileHandle ) ;

inline bool Open_StdioFile_Com(CString strFileName) ;
void Close_StdioFile( CStdioFile *pStdioFile ) ;

BOOL Read_StdioFile(CStdioFile *pStdioFile, CString &strReadData ) ;
void Write_StdioFile(CStdioFile *pStdioFile, char *pBufferData ) ;

int Seek_NumberOfPattLines( CString strFileName, CString &m_strEdit_Msg ) ;

extern int CALLBACK BrowseCallBackFun(HWND hwnd, UINT umsg, LPARAM lparam, LPARAM lpdata) ;

void Trans_UnicodeToHexStr( CString strUncode, CString& strHexStr ) ;
void Trans_AnsiToHexStr( CString strUncode, CString& strHexStr, int nSize );

// “CommFunction.cpp”

#include “stdafx.h”
//#include “afxcmn.h”
#include “CommFunction.h”

void SetCurrentDirectorySimple()
{
CString strPath;

GetModuleFileName(NULL, strPath.GetBuffer(MAX_PATH), MAX_PATH);
strPath.ReleaseBuffer();

int		pos;
pos = strPath.ReverseFind('\\');
strPath = strPath.Left(pos);
SetCurrentDirectory(strPath);

}

void trans_Unicode_To_Ansi(CString &strData, CString &strDest){
// 输入必须是Unicode,否则,输出编码会出错
if (!strData) return;

strDest = strData ;
int nLen = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)strData.GetBuffer(), -1, NULL, 0, NULL, NULL);
//int nLen = WideCharToMultiByte(NULL, 0, strData.GetBuffer(), -1, NULL, 0, NULL, NULL);
strDest.GetBuffer(nLen + 1);

LPSTR	lpwstr = (LPSTR)strDest.GetBuffer();
LPCWSTR	lpcch = (LPCWSTR)strData.GetBuffer();
WideCharToMultiByte(NULL, 0, lpcch, -1, lpwstr, nLen, NULL, FALSE);

lpwstr[nLen] = '\0';
memcpy( (void*)lpcch, strDest, nLen+1 ) ;

}

void UnicodeToAnsiSimple(CString &strData){
CString strDest;

trans_Unicode_To_Ansi(strData, strDest);

//strData = strDest;

}

void ExecuteApp(CString strParaGraph, CString strKeyName){
CString strPath, strIniFile, strAppFile;

GetModuleFileName(NULL, strPath.GetBuffer(MAX_PATH), MAX_PATH);
strPath.ReleaseBuffer();

int		pos;
pos = strPath.ReverseFind('\\');
strIniFile = strPath.Left(pos) + _T("\\SystemConfig.ini");

//AfxMessageBox(strParaGraph + _T(" ") + strKeyName + _T(" ") + strIniFile + _T(" ") + strAppFile);
SetCurrentDirectorySimple();
GetPrivateProfileString(strParaGraph.GetBuffer(), strKeyName.GetBuffer(), CString("NOTEPAD.EXE"), strAppFile.GetBuffer(MAX_PATH), MAX_PATH, strIniFile.GetBuffer());

UnicodeToAnsiSimple(strAppFile);

WinExec((LPCSTR)strAppFile.GetString(), SW_SHOW);

}

// paste Clp
char * ReadFromClipboard()
{
//打开剪贴板
if (!OpenClipboard(NULL))
return ("");

HANDLE hData = GetClipboardData(CF_TEXT);

char * pBuffer = (char*)GlobalLock(hData);
GlobalUnlock(hData);
CloseClipboard();

return pBuffer;

}

void AddToClipboard(CString m_editRead)
{
if (!OpenClipboard(AfxGetMainWnd()->GetSafeHwnd()))
return;

m_editRead.ReleaseBuffer();
if (m_editRead.IsEmpty() || m_editRead.GetLength() < 1)
	return;

m_editRead = _T("\t") + m_editRead;
CString		strTemp = m_editRead;
UnicodeToAnsiSimple(strTemp);
m_editRead.ReleaseBuffer();
int nNewLen = (int)strlen((char *)strTemp.GetBuffer());

HANDLE hClipMemory = GetClipboardData(CF_TEXT);
char * lpClipMemory = (char *)GlobalLock(hClipMemory);

int nOldLen = 0;
if (lpClipMemory)
	nOldLen = (int)strlen(lpClipMemory);

//AfxMessageBox( strSource ) ;

HGLOBAL hglbCopy = GlobalAlloc(GMEM_DDESHARE, (nOldLen + nNewLen + 1));
char *  lptstrCopy = (char *)GlobalLock(hglbCopy);

if (nOldLen > 0)
	memcpy(lptstrCopy, lpClipMemory, nOldLen);
if (nNewLen > 0)
		memcpy(lptstrCopy + nOldLen, strTemp, nNewLen);
EmptyClipboard(); 
lptstrCopy[nOldLen + nNewLen] = 0;

GlobalUnlock(hglbCopy);
SetClipboardData(CF_TEXT, hglbCopy);

CloseClipboard();

}

void ClearClipboardContent()
{
if (!OpenClipboard(AfxGetMainWnd()->GetSafeHwnd()))
return;

EmptyClipboard();

CloseClipboard();

}

void WriteToClipboard(CString m_editRead)
{
m_editRead.ReleaseBuffer();
if (m_editRead.IsEmpty() || m_editRead.GetLength() < 1)
return;

CString		strTemp = m_editRead;
UnicodeToAnsiSimple(strTemp);
int nNewLen = (int)strlen((char *)strTemp.GetBuffer());
if (!nNewLen)
	return;

if (!OpenClipboard(AfxGetMainWnd()->GetSafeHwnd()))
	return;

HGLOBAL hglbCopy = GlobalAlloc(GMEM_MOVEABLE, (nNewLen + 1));
char *  lptstrCopy = (char *)GlobalLock(hglbCopy);

memcpy(lptstrCopy, (char *)strTemp.GetBuffer(), nNewLen );
EmptyClipboard();
lptstrCopy[nNewLen] = 0;

GlobalUnlock(hglbCopy);
SetClipboardData(CF_TEXT, hglbCopy);

CloseClipboard();

}

void trans_Ansi_To_Unicode(CString &strData, CString &strDest){
// 输入必须是Ansi,否则,输出编码会出错. 待测试… …
char *p = (char *)strData.GetBuffer();
if (p == 0 || *p == 0) return;

int nLenOfData = strlen(p), nLenOfDest = (nLenOfData + 1) * sizeof(TCHAR);
strDest.GetBuffer(nLenOfDest);

LPWSTR	lpwstr = (LPWSTR)(LPCTSTR)strDest.GetBuffer();
LPCCH	lpcch = (LPCCH)(LPCTSTR)strData.GetBuffer();
MultiByteToWideChar(NULL, 0, lpcch, -1, lpwstr, nLenOfDest);

}

void trans_Multicode_To_Ansi(CString &strData, CString &strDest){
// 输入必须是Unicode,否则,输出编码会出错

char *p = (char *)strData.GetBuffer();
int j = 0;
for (int i = 0; i < strData.GetAllocLength(); i++){
	if (p[i] != 0)
		p[j] = p[i];
	j++;
}
p[j] = 0;
char *p0 = (char *)strDest.GetBuffer();
p0 = p;

}

// Ansi_To_Uncdode
bool Ansi_To_Uncdode( CString& lpwTarget, LPSTR lpSource )
{
DWORD dwMinSize = MultiByteToWideChar (CP_ACP, 0, lpSource, -1, NULL, 0);

CString		strTemp ;
strTemp.GetBuffer( dwMinSize*2 ) ;
lpwTarget.GetBuffer( dwMinSize*2 ) ;

LPWSTR	lpwDest = (LPWSTR)(LPCSTR)strTemp.GetBuffer() ;
MultiByteToWideChar( CP_ACP, 0, lpSource, -1, (LPWSTR)(LPCSTR)lpwDest, dwMinSize );

lpwTarget  = lpwDest;
//::AfxMessageBox( lpwTarget ) ;
//::AfxMessageBox( (CString)lpwDest ) ;

return true ;

}

// Ansi_To_Uncdode
bool Ansi_To_Uncdode( CString& lpwTarget )
{
CString strSource = lpwTarget ;
LPSTR lpSource = (LPSTR)strSource.GetString() ; // GetBuffer
DWORD dwMinSize = MultiByteToWideChar (CP_ACP, 0, lpSource, -1, NULL, 0);

CString		strTemp ;
strTemp.GetBuffer( dwMinSize*2 ) ;
lpwTarget.GetBuffer( dwMinSize*2 ) ;

LPWSTR	lpwDest = (LPWSTR)(LPCSTR)strTemp.GetBuffer() ;
MultiByteToWideChar( CP_ACP, 0, lpSource, -1, (LPWSTR)(LPCSTR)lpwDest, dwMinSize );

lpwTarget  = lpwDest;
//::AfxMessageBox( lpwTarget ) ;
//::AfxMessageBox( (CString)lpwDest ) ;

return true ;

}

// Uncdode_To_Ansi
bool Uncdode_To_Ansi(CString& strData )
{
// 输入必须是Unicode,否则,输出编码会出错
if (!strData) return false;
int nLen = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)strData.GetBuffer(), -1, NULL, 0, NULL, NULL);

CString strDest  ;	
strDest.GetBuffer( nLen + 1 );

LPSTR	lpwstr = (LPSTR)(LPCTSTR)strDest.GetBuffer();
LPCWCH	lpcch = (LPCWCH)(LPCTSTR)strData.GetBuffer();
WideCharToMultiByte(NULL, 0, lpcch, -1, lpwstr, nLen, NULL, FALSE);

//lpwstr[nLen] = '\0';
//memcpy(strData.GetBuffer(), lpcch, nLen+1)  ;
//::AfxMessageBox( (char *)lpcch ) ;
//::AfxMessageBox( strData ) ;

strDest = strData ;	

return true ;

}

CString Convert_DoubleToString( double dValue){
CString strTemp ;

strTemp.Format(_T("%f"), dValue ) ;

int		n = 0;
while (strTemp.Mid(strTemp.GetLength() - 1 - n, 1) == _T("0"))
	n++;
if (strTemp.Mid(strTemp.GetLength() - 1 - n, 1) == _T("."))
	n++;

strTemp = strTemp.Left( strTemp.GetLength() - n ) ;
strTemp.ReleaseBuffer() ;

return strTemp ;

}

/
/
/
打开文件
bool Open_File_Com(CString strFileName, DWORD& pFileHandle ){
// 文件已经打开
if (pFileHandle){
return true ;
}

str_FileName = strFileName ;
/ CFile::modeCreate |CFile::modeWrite| CFile::modeNoTruncate | CFile::modeNoTruncate
BOOL bRet = h_FileHandle.Open(strFileName,  CFile::modeWrite| CFile::modeNoTruncate | CFile::shareDenyNone );	
if ( bRet ){
	pFileHandle = (DWORD)&h_FileHandle ;
	return true ;
}

bRet = h_FileHandle.Open(strFileName,  CFile::modeCreate| CFile::modeWrite | CFile::shareDenyNone );	
if ( bRet ){
	pFileHandle = (DWORD)&h_FileHandle ;

	return true ;
}

AfxMessageBox( _T("提示,打开文件失败:") + strFileName );
pFileHandle = NULL ;
return false ;

}

void Close_File_Com( DWORD& pFileHandle){
CFile *pLocalHandle = (CFile *)(pFileHandle) ;
if (pLocalHandle)
pLocalHandle->Close( ) ;

pFileHandle = NULL ;

}

bool Open_File( CString strFileName, DWORD& dwFileHandle ){
bool ret = Open_File_Com( strFileName, dwFileHandle ) ;

p_FileHandle = (CFile *)dwFileHandle ;

return ret ;

}

void Write_File_Com( CFile *pFileHandle, char *strTemp ){
pFileHandle->SeekToEnd() ;
pFileHandle->Write( strTemp, (UINT)strlen(strTemp) );
}

// StdioFile ///

CStdioFile h_StdioFile, *pHandle_StdioFile = NULL;
CString m_strFileMsg ;

void Close_StdioFile( CStdioFile *pStdioFile ){
if ( !pStdioFile )
return ;

pStdioFile->Close() ;
pStdioFile = NULL ;

//pHandle_StdioFile->Close() ;
pHandle_StdioFile = NULL ;

}

//bool Open_StdioFile_Com(CString strFileName, CStdioFile stadioFile_Local, DWORD& pFileHandle ){
bool Open_StdioFile_Com(CString strFileName ){
if ( pHandle_StdioFile )
return false;

BOOL bRet = h_StdioFile.Open(strFileName,  CStdioFile::modeReadWrite | CStdioFile::shareDenyNone   ) ;  //CStdioFile::modeReadWrite || CStdioFile ::modeCreate CFile::modeRead | CFile::modeReadWrite |  | CFile::modeWrite CFile::shareDenyNone CStdioFile::modeCreate
if ( !bRet ){
	m_strFileMsg = _T("操作提示,打开文件失败:") + strFileName ;
	return false ;
}
else
	m_strFileMsg = _T("操作提示,打开文件成功.") + strFileName ;
	
h_StdioFile.SeekToBegin() ;
pHandle_StdioFile = &h_StdioFile ;

return true ; 

//CString strReadData ;
//strReadData.GetBuffer(MAX_PATH) ;

//h_StdioFile.SeekToBegin() ;
//pHandle_StdioFile = &h_StdioFile ;
//pHandle_StdioFile->ReadString( strReadData  ) ;
Close_StdioFile(pHandle_StdioFile) ;

}

BOOL Read_StdioFile(CStdioFile *pStdioFile, CString &strData ){
if ( !pHandle_StdioFile )
return false;

pHandle_StdioFile->ReadString( strData ) ;
//AfxMessageBox( strData );

return true ;

}

void Write_StdioFile(CStdioFile *pStdioFile, char *pBufferData ){
if ( !pStdioFile )
return ;

pStdioFile->SeekToEnd() ;
pStdioFile->Write(pBufferData, (UINT)strlen(pBufferData));

}

int Seek_NumberOfPattLines( CString strFileName, CString &m_strEdit_Msg ){
if (!Open_StdioFile_Com(strFileName )){

	m_strEdit_Msg = _T("打开文件失败!") + strFileName ;
	return 0;
}

int		nLines = 0 ;
CString strReadData, strTemp ;
while (Read_StdioFile(pHandle_StdioFile, strReadData )){
	if (strReadData.TrimLeft().MakeUpper().Left(2) != _T("FC"))
		continue ;

	strTemp.Format(_T(" %d "), nLines ) ;
	m_strEdit_Msg +=  strTemp ;
	m_strEdit_Msg +=  strReadData ;
	m_strEdit_Msg += _T("\r\n") ; 
	nLines++;
};

strReadData.Format(_T("文件共 %d 行"), nLines ) ;
m_strEdit_Msg +=  strReadData ;
m_strEdit_Msg += _T("\r\n") ; 

Close_StdioFile( pHandle_StdioFile ) ;
return nLines ;

}

void Select_PattFileFold( CString strFoldName ){
//CString strFoldName ; // = _T(“请选择要转换的全部文件所在的目录:”)
BROWSEINFOA binfo = { 0 } ;
//ZeroMemory(&binfo, sizeof(BROWSEINFO));

strFoldName.GetBuffer(MAX_PATH);
GetCurrentDirectory(MAX_PATH, strFoldName.GetBuffer());

binfo.ulFlags = BIF_RETURNONLYFSDIRS | BIF_USENEWUI;
binfo.lpfn = BrowseCallBackFun;
binfo.lParam = (LPARAM)strFoldName.GetBuffer();
//binfo.lpszTitle = (LPCWSTR)strPathName.GetBuffer();
binfo.lpszTitle = (LPCSTR)("请选择要转换的全部文件所在的目录1:");

LPITEMIDLIST lp = SHBrowseForFolderA( &binfo );
if (!lp | !SHGetPathFromIDList(lp, strFoldName.GetBuffer()))
	return;

SetCurrentDirectory(strFoldName.GetBuffer());

AfxMessageBox(strPathName + _T("\\target"));
//strPathName.ReleaseBuffer();
//strPathName = strPathName + _T("\\target");
//CreateDirectory(_T("target"), NULL);
AfxMessageBox(strTempPathName);
//ShellExecute(NULL, _T("open"), strPathName, _T(""), strPathName, 1);

}

int CALLBACK BrowseCallBackFun(HWND hwnd, UINT umsg, LPARAM lparam, LPARAM lpdata)
{
switch (umsg)
{
case BFFM_INITIALIZED:
::SendMessage(hwnd, BFFM_SETSELECTION, true, lpdata);
break;
default:
break;
}
return 0;
}

void Trans_AnsiToHexStr( CString strUncode, CString& strHexStr, int nSize ){
char strDest[4] ;
char *pBuffer, *pSource ;

strHexStr = _T("") ;
pSource = (char *)strUncode.GetBuffer() ;
	
for (int i = 0; i < nSize; i++){
	unsigned	char	ch = *pSource ;
	pSource++ ;

	sprintf( strDest, "%02X \0", ch ) ;
	pBuffer = &strDest[ 0 ] ;
	CString		strTemp ;
	strTemp = pBuffer ;
	strHexStr += strTemp ;
}

}

UNICODE 转 Hex字符串
void Trans_UnicodeToHexStr( CString strUncode, CString& strHexStr )
{
strHexStr = _T("") ;
int nSize = strUncode.GetLength( ) ;
char *pBuffer = (char *)strUncode.GetBuffer() ;

char	strDest[4] ;
for (int i = 0; i < nSize*2; i++){
	unsigned	char	ch = *pBuffer ; 
	pBuffer++ ;

	sprintf( strDest, "%02X \0", ch ) ;
	char *pData = &strDest[ 0 ] ;
	CString		strTemp ;
	strTemp = pData ;
	strHexStr += strTemp ;
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值