Shop OBEX P1 Docs P2 Docs Learn Events
How do I represent a byte array position value into binary string? — Parallax Forums

How do I represent a byte array position value into binary string?

Don MDon M Posts: 1,652
edited 2021-03-10 22:38 in Propeller 1

Let's say I have a byte array of 16 bytes and also a 2 byte array. If any one of those 16 bytes is an $FF then I would want the corresponding position of one of the bits set in the 2 byte array. I know it's hard to explain.

Byte array of 16: $00, $03, $05, $FD, $FF, $FF, $EE, $FF, $09, $00, $03, $05, $FD, $FF, $50, $09
then the 2 byte array would look like this in binary: 0000 1101 0000 01000

So how do I go about testing the bytes in the array and then setting a bit in the 2 byte array (or 1 word) if it's an $FF?

Comments

  • dgatelydgately Posts: 1,630
    edited 2021-03-10 23:39

    @"Don M" said:
    Let's say I have a byte array of 16 bytes and also a 2 byte array. If any one of those 16 bytes is an $FF then I would want the corresponding position of one of the bits set in the 2 byte array. I know it's hard to explain.

    Byte array of 16: $00, $03, $05, $FD, $FF, $FF, $EE, $FF, $09, $00, $03, $05, $FD, $FF, $50, $09
    then the 2 byte array would look like this in binary: 0000 1101 0000 01000

    So how do I go about testing the bytes in the array and then setting a bit in the 2 byte array (or 1 word) if it's an $FF?

    In spin (probably much more efficient ways to do this, but this explains how):

    '' Test  RE:  https://forums.parallax.com/discussion/173110/how-do-i-represent-a-byte-array-position-value-into-binary-string
    
    con
      _clkmode = xtal1 + pll16x
      _clkfreq = 80_000_000
    
    obj
        term : "fullduplexserial"
    
    var
        byte arrayBytes[16]
        word bitArray
    
    dat
        dataBytes byte  $00, $03, $05, $FD, $FF, $FF, $EE, $FF, $09, $00, $03, $05, $FD, $FF, $50, $09 
    
    
    pub main() | idx, mask
    
      term.start(63, 62, 0, 115_200)            '' terminal setup
      term.str(string("Testing..."))
      term.tx(13)
      term.tx(10)
    
      bytemove(@arrayBytes, @dataBytes, 16)
    
        repeat idx from 15 to 0
            if arrayBytes[idx] == $FF
                mask := 1 << (15 - idx)
                bitArray |= mask
    
        term.bin(bitArray, 16)
    
    Downloading file to port /dev/cu.usbserial-DN02MWJI
    2664 bytes sent                  
    Verifying RAM
    Download successful!
    [ Entering terminal mode. Type ESC or Control-C to exit. ]
    Testing...
    0000110100000100
    

    dgately

  • Don MDon M Posts: 1,652

    Thanks. Much appreciated.

  • Don MDon M Posts: 1,652

    One more question.. leaving the dataBytes in the order they are in your example how do I reverse the order in the bitArray so it comes out 0010000010110000 ? I tried reversing the "repeat idx from 15 to 0" to "0 to 15" but it was still the same.

  • Don MDon M Posts: 1,652

    Figured it out. Changed mask := 1 << (15 - idx) to mask := 1 << ( idx)

    Thanks.

  • @"Don M" said:
    Figured it out. Changed mask := 1 << (15 - idx) to mask := 1 << ( idx)

    Thanks.

    Welcome...

Sign In or Register to comment.