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

推荐订阅源

T
Threat Research - Cisco Blogs
S
Securelist
H
Heimdal Security Blog
Scott Helme
Scott Helme
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
Spread Privacy
Spread Privacy
Cyberwarzone
Cyberwarzone
V
Vulnerabilities – Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
C
CERT Recently Published Vulnerability Notes
P
Proofpoint News Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
人人都是产品经理
人人都是产品经理
C
Cisco Blogs
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Engineering at Meta
Engineering at Meta
Project Zero
Project Zero
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
有赞技术团队
有赞技术团队
T
Tailwind CSS Blog
Cisco Talos Blog
Cisco Talos Blog
Last Week in AI
Last Week in AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
O
OpenAI News
P
Proofpoint News Feed
Google Online Security Blog
Google Online Security Blog
Recent Announcements
Recent Announcements
Hacker News: Ask HN
Hacker News: Ask HN
美团技术团队
Stack Overflow Blog
Stack Overflow Blog
U
Unit 42
P
Privacy International News Feed
Google DeepMind News
Google DeepMind News
G
GRAHAM CLULEY
Apple Machine Learning Research
Apple Machine Learning Research
TaoSecurity Blog
TaoSecurity Blog
S
Security @ Cisco Blogs
C
Check Point Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Jina AI
Jina AI
S
Secure Thoughts
G
Google Developers Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LINUX DO - 最新话题
T
Tenable Blog
Latest news
Latest news
I
InfoQ

博客园 - 钢钢

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日。