本文介绍了C#字节[]以BCD和BCD到INT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一下现金出纳机机创建一个十六进制文件。我要读这个文件中。

I have a Hex file created by CashRegister Machine. I have to read this file in.

文件使用格式化详述如下。它像座包。

File uses formatting detailed below. It is like socket packets.

code数据:2字节结果
PLU code数据:7字节结果
单价数据:5字节结果
量数据:5字节结果
总额数据:5字节结果
PLU名称数据:18字节结果
税率资料:1字节结果
长度:24 + 19字节

Code Data : 2 Byte
PLU Code Data: 7 Byte
Unit Price Data: 5 Byte
Quantity Data: 5 Byte
Total Amount Data: 5 Byte
PLU Name Data: 18 Byte
Tax Rate Data: 1 Byte
Length: 24 + 19 Byte


  • PLU code格式是BCD

  • 单价1-9999999999(BCD)

  • 量1-9999999999(BCD最后3个数字应该是十进制)

  • 总量1-9999999999(BCD)

我在使用二进制读者hex文件读取,然后插入INT单价字节数组。

I read in the hex file with a binary reader and then insert int the Unit Price byte array.

byte[] bytes = { data[21], data[22], data[23], data[24], data[25] }; // BCD Byte Array

这阵单价。但是,我怎么能那么转换这个数字为十进制。而信息说,对于数量:BCD最后一个数字应该是小数 - 这是什么意思?谢谢你。

This array is Unit Price. But how can I then convert this number to decimal. And the information says that for quantity : BCD last number should be decimal--what does this mean? Thanks.

推荐答案

一个BCD数EN codeS 0-9的数值为4位。在压缩BCD(你处理大概是什么用),一个字节用来包含字节的两个值0-9,一个在每个半字节(4位)。要转换为int,你必须做的一点点摆弄。例如,下面将BCD转换成一个字节数组到一个int,它最多可容纳9位。如果你有超过9 BCD数字输入使用长。

A BCD number encodes a value from 0-9 into 4 bits. In packed BCD (probably what you're dealing with), a byte is used to contain two values 0-9, one in each nibble (4 bits) of the byte. To convert to an int, you have to do a little bit fiddling. For example, the following would convert an array of BCD bytes into an int, which can hold up to 9 digits. Use long if you have more than 9 bcd digit of input.

// assume byte[] bcds is input
int result = 0;
foreach(byte bcd in bcds) {
    result *= 100;
    result += (10 * (bcd >> 4));
    result += bcd & 0xf;
}

此假设每个字节被存储为大端BCD,这里最显著位是在该字节的最显著半字节。这就是在 。如果你正在处理little-endian的BCD内转换code中的for循环会

This assumes that each byte is stored as big-endian BCD, where the most significant digit is in the most significant nibble of the byte. This is what is described in the Wikipedia page for BCD as the more common implementation. If you are dealing with little-endian BCD, the conversion code within the for loop would be

    result *= 100;
    result += (10 * (bcd & 0xf));
    result += bcd >> 4;

您还需要确保你有你的阵列,即正确的顺序,并数组中的第一个字节包含最显著两位数,或至少显著两位数。例如,数123456将融入使用压缩BCD 3个字节。在字节[0]或字节[2] 12?你可能需要调整上述循环顺序颠倒,如果你的字节顺序是比我的假设不同。我假设12字节[0](大端,在最左边的字节最显著位)。

You also need to ensure you have the correct endianness of your array, i.e., does the first byte in the array contain the most significant two digits, or the least significant two digits. For example, the number 123456 would fit into 3 bytes using packed BCD. Is 12 in byte[0] or byte[2]? You would need to adjust the loop above to reverse the order if your endianness is different than my assumption. I'm assuming 12 is in byte[0] (big endian, with the most significant digits in the leftmost byte).

至于形容为BCD和十进制数量,我需要看到实际值,以了解他们在说什么。

As for the quantity described as BCD and decimal, I would need to see actual values to understand what they're talking about.

这篇关于C#字节[]以BCD和BCD到INT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 01:17