Home AMX User Forum NetLinx Studio

Binary Coded Decimal - BCD

Searched and found nothing...wondering if BCD has not been discussed here before...maybe on the ACE forums (will I ever go back and re-qualify?)...

Anyway, I am using a for-loop to decode BCD data; is that the most efficient way to glean BCD data in Netlinx?

Thx!

Comments

  • a_riot42a_riot42 Posts: 1,624
    roognation wrote: »
    Searched and found nothing...wondering if BCD has not been discussed here before...maybe on the ACE forums (will I ever go back and re-qualify?)...

    Anyway, I am using a for-loop to decode BCD data; is that the most efficient way to glean BCD data in Netlinx?

    Thx!

    is this a homework question? Lol

    You can use a while loop instead but I don't see any way of not looping through the data if that is what you are trying to avoid. As far as decoding is concerned there is likely various methods to do this depending on how many integers per byte, etc.
    Paul
  • PhreaKPhreaK Posts: 966
    I wrote a BCD encode function for the NCL math library if you're wanting to go the other way but haven't had a use for a decode function yet. If you end up putting one together it'd be awesome if you wanted to add it.
  • roognationroognation Posts: 138
    All I am doing now is evaluating a single CHAR against a list in a for-loop...when a match is found, the LOOP count equals the BCD (e.g. $14 is in the 14th position of the array I am testing).

    It would be nice to put something more robust together (and include in the Google repository), but I am off and running with what I have for now.

    Thx!
  • jweatherjweather Posts: 320
    The simplest format packs two decimal digits in each 8-bit byte, so a byte of $24 is actually decimal 24.
    // convert one byte
    define_function integer bcdbyte(char bcd) {
      return (bcd>>4)*10 + (bcd & $0F);
    }
    
    // convert four bytes
    define_function long bcdword(long bcd) {
      integer nib
      long dec
      dec = 0
      for (nib = 1; nib <= 8; nib++) {
        dec = dec*10 + ((bcd>>(32-nib*4))&$0F)
      }
      return dec
    }
    
  • roognationroognation Posts: 138
    jweather wrote: »
    The simplest format packs two decimal digits in each 8-bit byte, so a byte of $24 is actually decimal 24.
    // convert one byte
    define_function integer bcdbyte(char bcd) {
      return (bcd>>4)*10 + (bcd & $0F);
    }
    
    // convert four bytes
    define_function long bcdword(long bcd) {
      integer nib
      long dec
      dec = 0
      for (nib = 1; nib <= 8; nib++) {
        dec = dec*10 + ((bcd>>(32-nib*4))&$0F)
      }
      return dec
    }
    

    Nice and compact, thanks for the tip! I'll install this and test later today.
Sign In or Register to comment.