BCD为二进制和十六进制。 [英] BCD to binary and hex.

查看:116
本文介绍了BCD为二进制和十六进制。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很乐意问。有人知道将二进制编码的十进制转换为Bin以及十六进制的公式。问候。

解决方案

你可以看看 http://electriciantraining.tpub.com/14185/css/14185_71.htm [ ^ ]。


如何about(C#):

  int  i = 0x12345678;  //  假设:BCD编码 
int v = 0 ;
foreach byte b in BitConverter.GetBytes(i).Reverse())
{
int r =(b& 0x0F)+ 10 *((b>> 4 )& 0x0F);
v * = 100 ;
v + = r;
Console.WriteLine( 0x {0:X2} - > {1} = 0x {1: X2},b,r);
}
Console.WriteLine( 0x {0:X8} - > {1} = 0x {1:X8},i,v);

这导致:

 0x12  - > 12 = 0x0C 
0x34 - > 34 = 0x22
0x56 - > 56 = 0x38
0x78 - > 78 = 0x4E
0x12345678 - > 12345678 = 0x00BC614E



但不是速度优化。





您可以尝试按如下方式进行优化:

1) 10 * x =(8 + 2)* x =(x << 3)+(x< < 1)

2) 10 *(b>> 4)=((b>> 4)<< ; 3)+((b>> 4)<< 1)

3) 10 *(b>> 4)=(b>> 1)+(b>> 3)

因为右移是算术移位,并且因为移位的4位需要与其余部分隔离,我们必须将右移的结果掩盖到位模式中的相应4位:

4) 10 *(b>> 4 )=((b>> 1)& 0x78)+((b>> 3)& 0x1E)



类似地,v * = 100可以被优化:

1) v * = 100 - > v * =(64 + 32 + 4) - > v =(v << 6)+(v << 5)+(v << 2)



全部放在一起:

  int  i = 0x12345678; 
int v = 0 ;
foreach byte b in BitConverter.GetBytes(i).Reverse())
{
v =(v<< 6 )+(v<< ; 5 )+(v< 2 ); // 下一个字节:乘以100
v + =(b& 0x0F)+( (b>> 1 )& 0x78)+((b>> 3 ) & 0x1E); // 0xUV = 0x0V + 10 *(0xU0 / 16)= 0x0V + 1/2 * 0xU0 + 1/8 * 0x000
}
Console.WriteLine( 0x {0:X8} - > {1} = 0x {1:X8},i,v);



[/ EDIT]



干杯

Andi


非常感谢。现在我得到它将Bcd转换为hex你只需将其替换为Bin等效,然后将其转换为十六进制。非常感激。有帮助的。

I have small favor to ask.Anybody know the formula to convert binary coded decimal to Bin as well as hex.Kind regards.

解决方案

You can check out http://electriciantraining.tpub.com/14185/css/14185_71.htm[^].


How about (C#):

int i = 0x12345678; // assuming: BCD encoded
int v = 0;
foreach (byte b in BitConverter.GetBytes(i).Reverse())
{
    int r = (b & 0x0F) + 10 * ((b >> 4) & 0x0F);
    v *= 100;
    v += r;
    Console.WriteLine("0x{0:X2} -> {1} = 0x{1:X2}", b, r);
}
Console.WriteLine("0x{0:X8} -> {1} = 0x{1:X8}", i, v);

This results in:

0x12 -> 12 = 0x0C
0x34 -> 34 = 0x22
0x56 -> 56 = 0x38
0x78 -> 78 = 0x4E
0x12345678 -> 12345678 = 0x00BC614E


Not speed optimized, though.

[EDIT]
You may try to optimize as follows:
1) 10 * x = (8 + 2) * x = (x << 3) + (x << 1)
2) 10 * (b >> 4) = ((b >> 4) << 3) + ((b >> 4) << 1)
3) 10 * (b >> 4) = (b >> 1) + (b >> 3)
Since right shift is arithmetic shift, and since the shifted 4 bits need to be isolated from the rest, we must mask the results of the right shift to the respective 4 bits in the bit pattern:
4) 10 * (b >> 4) = ((b >> 1) & 0x78) + ((b >> 3) & 0x1E)

Similarily, v *= 100 can be optimized:
1) v *= 100 --> v *= (64 + 32 + 4) --> v = (v << 6) + (v << 5) + (v << 2)

Putting all together:

int i = 0x12345678;
int v = 0;
foreach(byte b in BitConverter.GetBytes(i).Reverse())
{
    v = (v << 6) + (v << 5) + (v << 2); // next byte: multiply by 100
    v += (b & 0x0F) + ((b >> 1) & 0x78) + ((b >> 3) & 0x1E); // 0xUV = 0x0V + 10 * (0xU0 / 16) = 0x0V + 1/2*0xU0 + 1/8*0xU0
}
Console.WriteLine("0x{0:X8} -> {1} = 0x{1:X8}", i, v);


[/EDIT]

Cheers
Andi


Thanks a lot. Now I get it to convert Bcd to hex u just replace each with its Bin equivalent then convert that to hex. Much appreciated. Helpful.


这篇关于BCD为二进制和十六进制。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆