不废话了来代码
public static byte ConvertBCD(byte b)
{
//高四位
byte b1 = (byte)(b / 10);
//低四位
byte b2 = (byte)(b % 10);
return (byte)((b1 << 4) | b2);
}
/// <summary>
/// 将BCD一字节数据转换到byte 十进制数据
/// </summary>
/// <param name="b" />字节数
/// <returns>返回转换后的BCD码</returns>
public static byte ConvertBCDToInt(this byte b)
{
//高四位
byte b1 = (byte)((b >> 4) & 0xF);
//低四位
byte b2 = (byte)(b & 0xF);
return (byte)(b1 * 10 + b2);
}
public static int ToBCD(byte val) {
int res = 0;
int bit = 0;
while (val >= 10) {
res |= (val % 10 << bit);
val /= 10;
bit += 4;
}
res |= val << bit;
return res;
}
public static byte FromBCD(int vals) {
int c = 1;
byte b = 0;
while (vals > 0) {
b += (byte) ((vals & 0xf) * c);
c *= 10;
vals >>= 4;
}
return b;
}