📄 mapcode.cpp
字号:
// MapCode.cpp : implementation file
//
#include "stdafx.h"
#include "PrjMapCode.h"
#include "MapCode.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMapCode dialog
CMapCode::CMapCode(CWnd* pParent /*=NULL*/)
: CDialog(CMapCode::IDD, pParent)
{
//{{AFX_DATA_INIT(CMapCode)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CMapCode::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMapCode)
DDX_Control(pDX, IDC_EDIT3, m_EDIT3);
DDX_Control(pDX, IDC_EDIT2, m_EDIT2);
DDX_Control(pDX, IDC_EDIT1, m_EDIT1);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMapCode, CDialog)
//{{AFX_MSG_MAP(CMapCode)
ON_BN_CLICKED(IDOK, OnEncode)
ON_BN_CLICKED(IDC_BUTTON1, OnDeCode)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMapCode message handlers
void CMapCode::OnEncode()
{
// TODO: Add your control notification handler code here
CString strSrc;
m_EDIT1.GetWindowText(strSrc);
strSrc.TrimLeft();
strSrc.TrimRight();
int count = strSrc.GetLength();
char *pSrc = (char *)malloc(count);
for(int i = 0; i < count; i++)
{
pSrc[i] = strSrc[i];
}
int sum = ((count/3)+1)*4 +1;
if (count%3 == 0)
sum = (count/3)*4 +1;;
char *edst = (char *)malloc(sum);
Encode(pSrc,edst,count);
edst[sum-1] = '\0';
CString result = edst;
m_EDIT2.SetWindowText(result);
}
void CMapCode::OnDeCode()
{
// TODO: Add your control notification handler code here
CString strSrc;
m_EDIT2.GetWindowText(strSrc);
strSrc.TrimLeft();
strSrc.TrimRight();
int count = strSrc.GetLength();
char *pSrc2 = (char *)malloc(count+1);
for(int i = 0; i < count; i++)
{
pSrc2[i] = strSrc[i];
}
char *edst = (char *)malloc(count+1);
for(i = 0; i < count; i++)
{
edst[i] = ' ';
}
edst[i] = '\0';
Decode(pSrc2, edst,count);
CString result = edst;
int summ = result.GetLength();
result.TrimRight();
m_EDIT3.SetWindowText(result);
}
char* ch64="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_|";
// Base64 编码
char * CMapCode::Encode(char *src, char *edst, long srclen)
{
int n,buflen,i,j;
int pading = 0;
char *buf = NULL;
char *dst = NULL;
buflen = srclen;
n = srclen;
if(n%3!=0) /* pad with '=' by using a temp buffer */
{
pading=1;
buflen=n+3-n%3;
buf=(char *)malloc(buflen+1);
memset(buf,0,buflen+1);
memcpy(buf,src,n);
for(i=0;i<3-n%3;i++)
buf[n+i]='=';
}
else
{
buf = (char *)malloc(buflen + 1);
memset(buf,0,buflen + 1);
memcpy(buf,src,n);
}
dst=(char *)malloc(buflen*4/3+1);
memset(dst,0,buflen*4/3+1);
for(i=0,j=0;i<buflen;i+=3,j+=4)
{
dst[j]=(buf[i]&0xFC)>>2;
dst[j+1]=((buf[i]&0x03)<<4) + ((buf[i+1]&0xF0)>>4);
dst[j+2]=((buf[i+1]&0x0F)<<2) + ((buf[i+2]&0xC0)>>6);
dst[j+3]=buf[i+2]&0x3F;
}
for(i=0;i<buflen*4/3;i++) /* map 6 bit value to base64 ASCII character */
dst[i]=ch64[dst[i]];
if(pading)
free(buf);
memcpy(edst, dst, i*sizeof(char));
free(dst);
return edst;
}
char * CMapCode::Decode(char *src, char *edst,long srclen)
{ int n,i,j;
char *p;
char *dst;
n = srclen;
for(i=0;i<n;i++) /* map base64 ASCII character to 6 bit value */
{
p=(char *)strchr(ch64,src[i]);
if(!p)
break;
src[i] = p - ch64;
}
dst=(char *)malloc(n*3/4+1);
memset(dst,0,n*3/4+1);
for(i=0,j=0;i<n;i+=4,j+=3)
{
dst[j]=(src[i]<<2) + ((src[i+1]&0x30)>>4);
dst[j+1]=((src[i+1]&0x0F)<<4) + ((src[i+2]&0x3C)>>2);
dst[j+2]=((src[i+2]&0x03)<<6) + src[i+3];
}
memcpy(edst, dst, i*sizeof(char));
free(dst);
return edst;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -