号码字符串与BCD编码互转 c#
/// <summary>
/// 把号码用BCD进行压缩编码。
/// </summary>
/// <param name="Num8BitByte">The num8 bit byte.</param>
/// <returns></returns>
public static byte[] ByteArrayToBCD(byte[] Num8BitByte)//8位的ascii码
{
byte[] Num4bitByte = new byte[8];
Num4bitByte = BitConverter.GetBytes(0xffffffffffffffff);
for (int i = 0; i<Num8BitByte.Length; i++)
{
byte num =Convert.ToByte(Num8BitByte[i] - 0x30);
if (i % 2 == 0)
{
Num4bitByte[i / 2] = Convert.ToByte((Num4bitByte[i / 2] & 0xF0) | num);
}
else
{
Num4bitByte[i / 2] = Convert.ToByte((Num4bitByte[i / 2] &0x0F)| (num << 4));
}
}
return Num4bitByte;
}
/// <summary>
/// BCDs to string.
/// </summary>
/// <param name="bcdNum">The BCD num.</param>
/// <returns></returns>
public static string bcdToString(byte[] bcdNum)
{
string retString="";
byte[] byteChar = new byte[bcdNum.Length];
byteChar = BitConverter.GetBytes(0xffffffffffffffff);
byte tempHigh=0,tempLow=0;
int i = 0;
while (tempHigh != 0x0F && tempLow != 0xF0)
{
tempHigh = Convert.ToByte(bcdNum[i] & 0xF0);//取出高四位;
tempHigh = Convert.ToByte(tempHigh >> 4);
tempLow = Convert.ToByte((bcdNum[i] & 0x0F) << 4);
byteChar[i] = Convert.ToByte(tempLow | tempHigh);
i++;
}
string[] HexString=BitConverter.ToString(byteChar).Trim().Split('-');
foreach (string str in HexString)
{
retString += str.Trim();
}
int LastIndex = retString.IndexOf('F');
return retString = retString.Substring(0, LastIndex);
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2013-03-06 c# Winform UI异步委托
2013-03-06 GPS经纬度判断是否在圆、多边形、矩形内