号码字符串与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);
}
最新文章
- Jetson ARM SeetaFace编译
- WordPress上传含有中文文件出现乱码
- ECS挂载数据盘
- hihoCoder #1174 : 拓扑排序&#183;一 (判断循环图)
- Unity3D TestTool Part _1
- Desert King
- linux 定时执行shell
- 龙杰ACR122S NFC读卡器
- 在VS上配置OpenCV
- Android-----js和android的互调
- JAVA加密算法系列-AesEBC
- Day6 Numerical simulation of optical wave propagation之通过随机介质(如大气湍流)的传播(二)
- [mock open]PyUnit执行单元测试时使用字符串模拟文件对象
- 如何禁止某个linux用户访问某些文件夹及执行某些命令
- 【spring基础】AOP概念与动态代理详解
- linux 如何快速的查找日志中你所要查找的信息
- .net 多线程的使用(Thread)
- 《Java多线程编程核心技术》学习笔记
- Apache Rewrite(大小写)
- Sa身份登陆SQL SERVER失败的解决方案
热门文章
- C 调用 lua 函数
- centos 7 下安装haproxy
- C++函数指针和类成员函数指针
- PHPstorm配置远程及本地服务器
- PHP编译configure时常见错误
- 解决android studio引用远程仓库下载慢(转)
- Hadoop科普文—常见的45个问题解答 &;#183; Hadoop
- 怎么用ChemDraw Pro绘制不定域共轭环
- ios -完全实现代码设置 Could not find a storyboard named &#39;Main&#39; in bundle NSBundle
- MySQL文件和Innodb引擎文件