string _BcdText = Zgke.ConvertByte.BCD.GetBcdEncode(123456, Zgke.ConvertByte.BCD.BcdType.码8421);
ulong _Value;
if(Zgke.ConvertByte.BCD.GetBcdDecode(_BcdText,Zgke.ConvertByte.BCD.BcdType.码8421,out _Value))
{
MessageBox.Show(_Value.ToString(), _BcdText);
}
全部类
1. using System;
2. using System.Collections.Generic;
3. using System.Text;
4. using System.Data;
5.
6. namespace Zgke.ConvertByte
7. {
class BCD
{
public enum BcdType
{
码8421,
码5421,
码2421,
余3码,
余3循环码
}
/// <summary>
/// 或BCD表数据
/// </summary>
/// <returns></returns>
private static DataTable GetBcdTable()
{
DataTable _BCDTable = new DataTable();
_BCDTable.Columns.Add("十进制数");
_BCDTable.Columns.Add("码8421");
_BCDTable.Columns.Add("码5421");
_BCDTable.Columns.Add("码2421");
_BCDTable.Columns.Add("余3码");
_BCDTable.Columns.Add("余3循环码");
_BCDTable.Rows.Add(new object[] { "0", "0000", "0000", "0000", "0011", "0010" });
_BCDTable.Rows.Add(new object[] { "1", "0001", "0001", "0001", "0100", "0110" });
_BCDTable.Rows.Add(new object[] { "2", "0010", "0010", "0010", "0101", "0111" });
_BCDTable.Rows.Add(new object[] { "3", "0011", "0011", "0011", "0110", "0101" });
_BCDTable.Rows.Add(new object[] { "4", "0100", "0100", "0100", "0111", "0100" });
_BCDTable.Rows.Add(new object[] { "5", "0101", "1000", "1011", "1000", "1100" });
_BCDTable.Rows.Add(new object[] { "6", "0110", "1001", "1100", "1001", "1101" });
_BCDTable.Rows.Add(new object[] { "7", "0111", "1010", "1101", "1010", "1111" });
_BCDTable.Rows.Add(new object[] { "8", "1000", "1011", "1110", "1011", "1110" });
_BCDTable.Rows.Add(new object[] { "9", "1001", "1100", "1111", "1100", "1010" });
return _BCDTable;
}
/// <summary>
/// BCD解码
/// </summary>
/// <param name="p_Value">BCD码</param>
/// <param name="p_BcdType">BCD码类型</param>
/// <param name="p_LongValue">返回数据</param>
/// <returns>false 解码失败 true解编成功</returns>
public static ulong GetBcdDecode(string p_Value, BcdType p_BcdType)
{
DataTable _BCDTable = GetBcdTable();
ulong p_LongValue = 0;
StringBuilder _Return = new StringBuilder();
if (p_Value.Length % 4 == 0)
{
int _Count = p_Value.Length / 4;
string _ValueText = p_Value.ToString();
for (int i = 0; i != _Count; i++)
{
DataRow[] _Row = _BCDTable.Select(p_BcdType.ToString() + "=" + _ValueText.Substring(i * 4, 4));
if (_Row.Length == 0) return 0;
_Return.Append(_Row[0]["十进制数"].ToString());
}
p_LongValue = Convert.ToUInt64(_Return.ToString());
return p_LongValue;
}
return 0;
}
/// <summary>
/// BCD编码
/// </summary>
/// <param name="p_Value">数据</param>
/// <param name="p_BcdType">编码方式</param>
/// <returns>编码 ""</returns>
public static string GetBcdEncode(ulong p_Value, BcdType p_BcdType)
{
DataTable _BCDTable = GetBcdTable();
StringBuilder _Return = new StringBuilder();
string _ValueText = p_Value.ToString();
for (int i = 0; i != _ValueText.Length; i++)
{
DataRow[] _Row = _BCDTable.Select("十进制数=" + _ValueText[i].ToString());
if (_Row.Length == 0) return "";
_Return.Append(_Row[0][p_BcdType.ToString()].ToString());
}
return _Return.ToString();
}
}
C#实现BCD码
最新推荐文章于 2022-08-26 19:34:57 发布