惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
Vercel News
Vercel News
G
Google Developers Blog
Martin Fowler
Martin Fowler
大猫的无限游戏
大猫的无限游戏
U
Unit 42
H
Hackread – Cybersecurity News, Data Breaches, AI and More
S
Securelist
Schneier on Security
Schneier on Security
F
Full Disclosure
P
Proofpoint News Feed
C
Cisco Blogs
J
Java Code Geeks
K
Kaspersky official blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cisco Talos Blog
Cisco Talos Blog
小众软件
小众软件
博客园_首页
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
CXSECURITY Database RSS Feed - CXSecurity.com
Project Zero
Project Zero
Google DeepMind News
Google DeepMind News
Security Latest
Security Latest
D
DataBreaches.Net
MongoDB | Blog
MongoDB | Blog
阮一峰的网络日志
阮一峰的网络日志
W
WeLiveSecurity
AI
AI
V
V2EX
B
Blog RSS Feed
Google Online Security Blog
Google Online Security Blog
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog
腾讯CDC
P
Privacy & Cybersecurity Law Blog
月光博客
月光博客
T
Threat Research - Cisco Blogs
T
The Exploit Database - CXSecurity.com
S
Schneier on Security
IT之家
IT之家
Latest news
Latest news
The GitHub Blog
The GitHub Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Last Watchdog
The Last Watchdog
爱范儿
爱范儿
Y
Y Combinator Blog
TaoSecurity Blog
TaoSecurity Blog
aimingoo的专栏
aimingoo的专栏
S
Secure Thoughts

博客园 - 钢钢

Hadoop 调研笔记 使用SerialPort 对象实现串口拨号器通信[下] 使用SerialPort 对象实现串口拨号器通信[上] SQL Server 2005 创建分区表 考勤表生成工具介绍及使用说明 Modbus RTU 通信工具设计 SQL Server 中,实现 varbinary 与 varchar 类型之间的数据转换 使用C# 实现串口拨号器的SIM卡通信[修正版] 使用C# 实现串口拨号器的SIM卡通信 我的INI 配置文件读写动态库 关于ASP.NET 将数据导出成Excel 的总结[补充] 子角色权限的实现 两个横向菜单栏示例 C# 实现16进制和字符串之间转换的代码[转] 固定GridView 的表头和某几列 SQL Server 中几个有用的特殊函数 SQLServer 2005 XML 在 T-SQL 查询中的典型应用[转] 关于ASP.NET 将数据导出成Excel 的总结[下] 关于ASP.NET 将数据导出成Excel 的总结[中]
什么是BCD 码
钢钢 · 2012-04-23 · via 博客园 - 钢钢

BCD码(Binary-Coded Decimal‎)亦称二进码十进数或二-十进制代码。用4位二进制数来表示1位十进制数中的0~9这10个数码。是一种二进制的数字编码形式,用二进制编码的十进制代 码。BCD码这种编码形式利用了四个位元来储存一个十进制的数码,使二进制和十进制之间的转换得以快捷的进行。这种编码技巧最常用于会计系统的设计里,因 为会计制度经常需要对很长的数字串作准确的计算。相对于一般的浮点式记数法,采用BCD码,既可保存数值的精确度,又可免却使电脑作浮点运算时所耗费的时 间。此外,对于其他需要高精确度的计算,BCD编码亦很常用。

来源:http://baike.baidu.com/view/45179.htm

BCD 编码解码函数如下:

/// <summary>
/// BCD解码
/// </summary>
/// <param name="b"></param>
/// <returns></returns>
public static byte UnpackBCD(byte b)
{
    //高四位
    byte b1 = (byte)(b >> 4);
    //低四位
    byte b2 = (byte)(b & 0x0F);

    return (byte)(b1 * 10 + b2);
}


/// <summary>
/// BCD编码
/// </summary>
/// <param name="b"></param>
/// <returns></returns>
public static byte PackBCD(byte b)
{
    //高四位
    byte b1 = (byte)(b / 10);
    //低四位
    byte b2 = (byte)(b % 10);

    return (byte)((b1<<4)|b2);
}

BCD 编码测试,代码如下:

class Program
{
    // BCD编码
    public static byte PackBCD(byte b)
    {
        //高四位
        byte b1 = (byte)(b / 10);
        //低四位
        byte b2 = (byte)(b % 10);

        return (byte)((b1 << 4) | b2);
    }

    static void Main(string[] args)
    {
        byte[] buff = new byte[2];
        DateTime date = DateTime.Now;

        byte in_Month = (byte)(date.Month);
        byte in_Day = (byte)(date.Day);
        Console.WriteLine(in_Month.ToString() +"  "+ in_Day.ToString());

        buff[0] = PackBCD(in_Month);
        buff[1] = PackBCD(in_Day);

        Console.WriteLine(buff[0].ToString() + "  " + buff[1].ToString());
        Console.ReadKey();
    }
}

结果如下:

可以看出,月份4进行BCD编码后没有改变,23日进行BCD编码后变成了35。

BCD 解码测试,代码如下:

class Program
{
    // BCD解码
    public static byte UnpackBCD(byte b)
    {
        //高四位
        byte b1 = (byte)(b >> 4);
        //低四位
        byte b2 = (byte)(b & 0x0F);

        return (byte)(b1 * 10 + b2);
    }

    static void Main(string[] args)
    {
        byte[] buff = new byte[2] { 0x04, 0x23 };
        Console.WriteLine(buff[0].ToString() + "  " + buff[1].ToString());

        byte in_Month = UnpackBCD(buff[0]);
        byte in_Day = UnpackBCD(buff[1]);

        Console.WriteLine(in_Month.ToString() + "  " + in_Day.ToString());
        Console.ReadKey();
    }
}

结果如下:

可以看出,月份4进行BCD解码后没有改变,35进行BCD解码后还原成了23日。