The most efficient way to convert BCD to binary

I have the code below to convert a 32-bit BCD value (supplied in two halves of uint) to a binary uint value.

The values ​​given can be up to 0x9999 to form the maximum value 0x99999999.

Is there a better (faster) way to achieve this?

/// <summary> /// Convert two PLC words in BCD format (forming 8 digit number) into single binary integer. /// eg If Lower = 0x5678 and Upper = 0x1234, then Return is 12345678 decimal, or 0xbc614e. /// </summary> /// <param name="lower">Least significant 16 bits.</param> /// <param name="upper">Most significant 16 bits.</param> /// <returns>32 bit unsigned integer.</returns> /// <remarks>If the parameters supplied are invalid, returns zero.</remarks> private static uint BCD2ToBin(uint lower, uint upper) { uint binVal = 0; if ((lower | upper) != 0) { int shift = 0; uint multiplier = 1; uint bcdVal = (upper << 16) | lower; for (int i = 0; i < 8; i++) { uint digit = (bcdVal >> shift) & 0xf; if (digit > 9) { binVal = 0; break; } else { binVal += digit * multiplier; shift += 4; multiplier *= 10; } } } return binVal; } 
+5
source share
7 answers

If you have free space for an array of 39 322 elements, you can always just look up the value.

+8
source

If you expand the loop, remember to save the bit shift.

 value = ( lo & 0xF); value += ((lo >> 4 ) & 0xF) * 10; value += ((lo >> 8 ) & 0xF) * 100; value += ((lo >> 12) & 0xF) * 1000; value += ( hi & 0xF) * 10000; value += ((hi >> 4 ) & 0xF) * 100000; value += ((hi >> 8 ) & 0xF) * 1000000; value += ((hi >> 12) & 0xF) * 10000000; 
+6
source

Your code seems rather complicated; Do you need special error checking?

Otherwise, you can simply use the following code, which should not be slower, in fact, it is basically the same:

 uint result = 0; uint multiplier = 1; uint value = lo | hi << 0x10; while (value > 0) { uint digit = value & 0xF; value >>= 4; result += multiplier * digit; multiplier *= 10; } return result; 
+2
source

Try the following:

 public static int bcd2int(int bcd) { return int.Parse(bcd.ToString("X")); } 
+2
source

I assume you can expand the loop:

 value = ( lo & 0xF); value+= ((lo>>4) & 0xF) *10; value+= ((lo>>8) & 0xF) *100; value+= ((lo>>12)& 0xF) *1000; value+= ( hi & 0xF) *10000; value+= ((hi>>4 & 0xF) *100000; value+= ((hi>>8) & 0xF) *1000000; value+= ((hi>>12)& 0xF) *10000000; 

And you can check the invalid BCD digits as follows:

 invalid = lo & ((lo&0x8888)>>2)*3 

This sets an invalid value other than zero if any hexadecimal digit is> 9.

+1
source

Of course, there is a more efficient method. this is of course an example, so you can configure it as a lesson ^^

 function bcd_to_bin ($bcd) { $mask_sbb = 0x33333333; $mask_msb = 0x88888888; $mask_opp = 0xF; for($i=28;$i;--$i) { $mask_msb <<= 1; $mask_opp <<= 1; $mask_sbb <<= 1; for($j=0;$j<$i;$j+=4) { $mask_opp_j = $mask_opp << $j; if ($bcd & $mask_msb & $mask_opp_j ) { $bcd -= $mask_sbb & $mask_opp_j; } } } return $bcd; 

}

0
source
 public static uint BCDToNum(int num) { return uint.Parse(num.ToString(), System.Globalization.NumberStyles.HexNumber); } 
0
source

Source: https://habr.com/ru/post/889109/


All Articles