bcd

关于BCD码的编码和解码

自作多情 提交于 2020-03-04 23:37:57
(1)BCD码(二到十进制编码) 人们通常习惯使用十进制数,而计算机内部多采用二进制表示和处理数值数据, 因此在计算机输入和输出数据时,就要进行由十进制到二进制的转换处理。 把十进制数的每一位分别写成二进制形式的编码,称为二进制编码的十进制数, 即二到十进制编码或BCD(Binary Coded Decimal)编码。 BCD码编码方法很多,通常采用8421编码,这种编码方法最自然简单。 其方法使用四位二进制数表示一位十进制数,从左到右每一位对应的权分别是 23、22、21、20,即8、4、2、1。例如十进制数1975的8421码可以这样得出 1975(D)=0001 1001 0111 0101(BCD) 用四位二进制表示一位十进制会多出6种状态,这些多余状态码称为BCD码中的非法码。 BCD码与二进制之间的转换不是直接进行的, 当需要将BCD码转换成二进制码时,要先将BCD码转换成十进制码,然后再转换成二进制码; 当需要将二进制转换成BCD码时,要先将二进制转换成十进制码,然后再转换成BCD码。 编码过程,将数字69进行BCD编码(注:BCD编码低位在前,后面将不再注释)。 1. 将6,9分别转换成二进制表示:6(00000110)9(00001001),大家可以看到,最大的数字9也只要4个位,在传输过程中白白浪费了4个位; 2. 将69合并为一个字节,分别取6

蓝桥杯(单片机组)学习笔记(六)--------DS1302

家住魔仙堡 提交于 2020-02-15 01:58:44
1.DS1302 DS1302是低功耗实时时钟芯片,它可以对年、月、日、周、时、分、秒进行计时,且具有闰年补偿等多种功能。主要特点是采用串行数据传输,可为掉电保护电源提供可编程的充电功能,并且可以关闭充电功能。 1.引脚分析 原理图 DS1302是BCD码作为编码方式的,而且是压缩BCD码,8位表示0-99整数(Year = 99),7位表示0-79整数(Sec、Min = 60),6位表示0-49整数(Hour = 24、Date = 31),5位表示0-19整数(Month = 12),4位表示0-9整数(WeekDay = 7)。DS1302是变种SPI,通过SCLK和I/O相互配合,发送和接收信息。SCLK在上升沿前,主机通过I/O口发送数据,DS1302在上升沿是接收;SCLK在下降沿后,DS1302通过I/O口发送数据,主机接收。 使用同步串行通讯简化了DS1302 与微处理器的接口。与时钟/RAM 通讯只需要三根线: CE(也是原理图上的RST,SDA)(读写时必须保持高电平),I/O (数据线), and SCLK (串行时钟)。 2.寄存器结构 如上图,命令字启动每一次数据传输. MSB (位 7)必须是逻辑1. 如果是 0,则禁止对DS1302写入. 位 6 在逻辑0时规定为时钟/日历数据,逻辑1时为RAM数据.位 1 至 位 5 表示了输入输出的指定寄存器

COBOL COMP-3 number format issue

别来无恙 提交于 2020-01-16 05:38:07
问题 I have a cobol "tape format" dump which has a mixture of text and number fields. I'm reading the file in C# as a binary array (array of byte). I have the copy book and the formats are lining up fine on the text fields. There are a number of COMP-3 fields as well. The data in those fields doesnt seem to match any BCD format. I know what the data should be and I have the raw bytes of the COMP-3. I tried converting to EBCDIC first which yielded no better results. Any thoughts on how a COMP-3

7-4 BCD解密(10分)

荒凉一梦 提交于 2020-01-14 05:43:45
BCD数是用一个字节来表达两位十进制的数,每四个比特表示一位。所以如果一个BCD数的十六进制是0x12,它表达的就是十进制的12。 但是小明没学过BCD,把所有的BCD数都当作二进制数转换成十进制输出了。于是BCD的0x12被输出成了十进制的18了! 现在,你的程序要读入这个错误的十进制数,然后输出正确的十进制数。 提示:你可以把18转换回0x12,然后再转换回12。 输入格式: 输入在一行中给出一个[0, 153]范围内的正整数,保证能转换回有效的BCD数,也就是说这个整数转换成十六进制时不会出现A - F的数字。 输出格式: 输出对应的十进制数。 输入样例: 18 输出样例: 12 解答 # include <iostream> using namespace std ; int main ( ) { /* int n; cin >> n; int a[8]; int g = 0, s = 0; for (int i = 0; i < 8; i++) { a[i] = n % 2; n = n / 2; } g = a[0] + a[1] * 2 + a[2] * 2 * 2 + a[3] * 2 * 2 * 2; s = a[4] + a[5] * 2 + a[6] * 2 * 2 + a[7] * 2 * 2 * 2; cout << (s * 10 + g); */

Assembler : why BCD exists?

牧云@^-^@ 提交于 2020-01-13 07:49:12
问题 I know BCD is like more intuitive datatype if you don't know binary. But I don't know why to use this encoding, its like don't makes a lot of sense since its waste representation in 4bits (when representation is bigger than 9). Also I think x86 only supports adds and subs directly (you can convert them via FPU). Its possible that this comes from old machines, or other architectures? Thanks! 回答1: I think BCD is useful for many things, the reasons given above. One thing that is sort of obvious

4字节BCD码转4字节uint方法

天大地大妈咪最大 提交于 2020-01-06 15:28:43
u32 ch1=0; u32 int1=0; int1=0; int1= (u8) (str[9]/0x10); int1*=10;//BCD卡号变换为16进制 int1+=(u8)(str[9]%0x10); int1*=10; int1+=(u8)(str[10]/0x10); int1*=10; int1+=(u8)(str[10]%0x10); int1*=10; int1+=(u8)(str[11]/0x10); int1*=10; int1+=(u8)(str[11]%0x10); int1*=10; int1+=(u8)(str[12]/0x10); int1*=10; int1+=(u8)(str[12]%0x10); /*int1= (u8) (str[9]>>4); int1*=10;//BCD卡号变换为16进制 int1+=(u8)(str[9]&0x0f); int1*=10; int1+=(u8)(str[10]>>4); int1*=10; int1+=(u8)(str[10]&0x0f); int1*=10; int1+=(u8)(str[11]>>4); int1*=10; int1+=(u8)(str[11]&0x0f); int1*=10; int1+=(u8)(str[12]>>4); int1*=10; int1+=(u8)(str[12]

Convert really big number from binary to decimal and print it

与世无争的帅哥 提交于 2020-01-01 11:35:29
问题 I know how to convert binary to decimal. I know at least 2 methods: table and power ;-) I want to convert binary to decimal and print this decimal. Moreover, I'm not interested in this `decimal'; I want just to print it. But, as I wrote above, I know only 2 methods to convert binary to decimal and both of them required addition. So, I'm computing some value for 1 or 0 in binary and add it to the remembered value. This is a thin place. I have a really-really big number (1 and 64 zeros). While

BCD math library for arbitrary big numbers?

眉间皱痕 提交于 2019-12-30 08:28:29
问题 I'm looking for a replacement of the stock Delphi Data.FmtBcd library because I just hit its limits like maximum decimal digits it can represent and program terminates with EBcdOverflowException . For the curious, I'm calculating arithmetic series members and need to handle very large numbers - hundred-thousands positions are not so uncommon. And also get results in a reasonable time. I did rewritten part of the code to Python 3.2 for the testing purposes and calculation speed would be

Use C# BCD WMI Provider to SafeBoot Windows

巧了我就是萌 提交于 2019-12-23 05:32:21
问题 I have scoured the web looking for solutions on how to SafeBoot into Windows using only C#. Since Vista and above, safe booting is controlled using BCD. Ofcourse you could use the commandline tool "bcdedit": bcdedit /set {current} safeboot Minimal However I do not want to use this approach. So my question is: How do I reboot into safe mode using only C#? I have already looked at this SO post, which has got me started. But I'm still missing pieces to this puzzle. Any help is greatly

BCD Adder in Verilog

痴心易碎 提交于 2019-12-22 07:39:56
问题 I am trying to write a BCD Adder in Verilog, but I am having trouble with one of the modules. Specifically, the adder that takes two BCD digits and adds them. So, the idea is if the sum of the two digits is less than or equal to nine, then it is correct. However, if it is greater, then an offset of 6 has to be added. Here is my Verilog code so far: module DIGITADD( input [3:0] IN_A, input [3:0] IN_B, input CIN, output reg COUT, output reg [3:0] SUM ); wire s2, c2; always @ ( * ) begin assign