📄 linearcodedlg.cpp
字号:
// LinearCodeDlg.cpp : implementation file
//
#include "stdafx.h"
#include "LinearCode.h"
#include "LinearCodeDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CLinearCodeDlg dialog
CLinearCodeDlg::CLinearCodeDlg(CWnd* pParent /*=NULL*/)
: CDialog(CLinearCodeDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CLinearCodeDlg)
m_sData = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CLinearCodeDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CLinearCodeDlg)
DDX_Text(pDX, IDC_DATA, m_sData);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CLinearCodeDlg, CDialog)
//{{AFX_MSG_MAP(CLinearCodeDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_ENCODE, OnEncode)
ON_BN_CLICKED(IDC_DECODE, OnDecode)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLinearCodeDlg message handlers
BOOL CLinearCodeDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CLinearCodeDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CLinearCodeDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
bool GetA3(BYTE bRaw)
{
int num = 0;
// 与该监督位对应的信息位
int A3[5] = {3, 4, 5, 6, 7};
// 统计信息位的奇偶性
for (int i = 0; i < 5; i++)
{
int R = 1 << A3[i];
if ((bRaw & R) == R)
num++;
}
// 计算第1位监督位的取值,使之与对应信息位能够完成偶校验
float temp = num / 2.0f;
if (temp == (int)(num / 2))
return false;
else
return true;
}
bool GetA2(BYTE bRaw)
{
int num = 0;
// 与该监督位对应的信息位
int A2[5] = {0, 1, 2, 6, 7};
// 统计信息位的奇偶性
for (int i = 0; i < 5; i++)
{
int R = 1 << A2[i];
if ((bRaw & R) == R)
num++;
}
// 计算第1位监督位的取值,使之与对应信息位能够完成偶校验
float temp = num / 2.0f;
if (temp == (int)(num / 2))
return false;
else
return true;
}
bool GetA1(BYTE bRaw)
{
int num = 0;
// 与该监督位对应的信息位
int A1[5] = {1, 2, 4, 5, 6};
// 统计信息位的奇偶性
for (int i = 0; i < 5; i++)
{
int R = 1 << A1[i];
if ((bRaw & R) == R)
num++;
}
// 计算第1位监督位的取值,使之与对应信息位能够完成偶校验
float temp = num / 2.0f;
if (temp == (int)(num / 2))
return false;
else
return true;
}
bool GetA0(BYTE bRaw)
{
int num = 0;
// 与该监督位对应的信息位
int A0[5] = {0, 2, 3, 5, 7};
// 统计信息位的奇偶性
for (int i = 0; i < 5; i++)
{
int R = 1 << A0[i];
if ((bRaw & R) == R)
num++;
}
// 计算第1位监督位的取值,使之与对应信息位能够完成偶校验
float temp = num / 2.0f;
if (temp == (int)(num / 2))
return false;
else
return true;
}
void Encode(BYTE* pbRaw, BYTE* pbCode, int nRawLen)
{
// 中间变量
int point = 0;
int num = 0;
BYTE temp1 = 0;
BYTE temp2 = 0;
for (int i = 0; i < nRawLen; i++)
{
temp1 = pbRaw[i];
num += 8;
// 保存原始信息位和计算出来的监督位到编码流
if (num == 8)
{
pbCode[point] = temp1;
point++;
}
if (num == 20)
{
pbCode[point] = (unsigned char)((temp2 << 4) | (temp1 >> 4));
point++;
}
// 计算监督位
temp2 = 0;
temp1 = pbRaw[i];
if (GetA3(temp1) == true)
temp2 |= 8;
if (GetA2(temp1) == true)
temp2 |= 4;
if (GetA1(temp1) == true)
temp2 |= 2;
if (GetA0(temp1) == true)
temp2 |= 1;
num += 4;
// 指针修正
if (num == 24)
{
pbCode[point] = (unsigned char)((temp1 << 4) | temp2);
point++;
num = 0;
}
}
// 原始信息流为奇数时在编码流末尾做补0处理
if (num == 12)
pbCode[point] = (unsigned char)(temp2 << 4);
}
void Decode(BYTE* pbRaw, BYTE* pbCode, int nRawLen)
{
// 各编码位所对应的校正子取值,校正子为0时校验正确
BYTE A[12] = {1, 2, 4, 8, 5, 6, 7, 9, 10, 11, 14, 13};
// 中间变量
BYTE bRet;
int num = 0;
int point = 0;
int S0, S1, S2, S3;
bool temp1;
BYTE temp3, temp4;
BYTE bInfo = 0;
BYTE bCheck = 0;
for (int i = 0; i < nRawLen; i++)
{
// 提取信息位和监督位
if (num == 0)
{
bInfo = pbCode[point];
bCheck = pbCode[point + 1];
}
if (num == 12)
{
temp3 = pbCode[point];
temp4 = pbCode[point + 1];
bInfo = (unsigned char)((temp3 << 4) | (temp4 >> 4));
temp4 = pbCode[point + 1];
bCheck = (unsigned char)(temp4 << 4);
}
// 计算S3
temp1 = GetA3(bInfo);
temp3 = bCheck;
if (((temp3 & 0x80) >> 7) == 1)
{
if (temp1 == true)
S3 = 0;
else
S3 = 1;
}
else
{
if (temp1 == true)
S3 = 1;
else
S3 = 0;
}
// 计算S2
temp1 = GetA2(bInfo);
temp3 = bCheck;
if (((temp3 & 0x40) >> 6) == 1)
{
if (temp1 == true)
S2 = 0;
else
S2 = 1;
}
else
{
if (temp1 == true)
S2 = 1;
else
S2 = 0;
}
// 计算S1
temp1 = GetA1(bInfo);
temp3 = bCheck;
if (((temp3 & 0x20) >> 5) == 1)
{
if (temp1 == true)
S1 = 0;
else
S1 = 1;
}
else
{
if (temp1 == true)
S1 = 1;
else
S1 = 0;
}
// 计算S0
temp1 = GetA0(bInfo);
temp3 = bCheck;
if (((temp3 & 0x10) >> 4) == 1)
{
if (temp1 == true)
S0 = 0;
else
S0 = 1;
}
else
{
if (temp1 == true)
S0 = 1;
else
S0 = 0;
}
// 得到校正子
bRet = 0;
bRet |= S3 << 3;
bRet |= S2 << 2;
bRet |= S1 << 1;
bRet |= S0;
if (bRet == 0)
{
// 无需纠错
pbRaw[i] = bInfo;
}
else
{
// 得到错误位置
for (int j = 0; j < 12; j++)
{
if (A[j] == bRet)
break;
}
if (j <= 3)
{
// 监督位无须纠错
pbRaw[i] = bInfo;
}
else
{
// 信息位反码纠错
temp3 = bInfo;
temp3 &= 1 << (j - 4);
temp3 = (unsigned char)(~temp3);
pbRaw[i] = (unsigned char)((temp3 & bInfo) | (temp3 & (1 << (j - 4))));
}
}
// 修正计数器
point++;
num += 12;
if (num == 24)
{
num = 0;
point++;
}
}
}
void CLinearCodeDlg::OnEncode()
{
UpdateData(TRUE);
BYTE TempIn[1024];
BYTE TempOut[1024];
memcpy((char*)TempIn, m_sData, m_sData.GetLength());
memset((char*)TempOut,0,sizeof(TempOut));
Encode(TempIn,TempOut,m_sData.GetLength());
m_sData = CString(TempOut);
UpdateData(FALSE);
}
void CLinearCodeDlg::OnDecode()
{
UpdateData(TRUE);
BYTE TempIn[1024];
BYTE TempOut[1024];
memcpy((char*)TempIn, m_sData, m_sData.GetLength());
memset((char*)TempOut,0,sizeof(TempOut));
int nRawLen = (int)(m_sData.GetLength() * 2 / 3.0f);
Decode((LPBYTE)TempOut, (LPBYTE)TempIn, nRawLen);
m_sData = CString(TempOut);
UpdateData(FALSE);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -