Announcement

Collapse
No announcement yet.

Bit analysis of string

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Bit analysis of string

    A newbie question; I recieve data from the serial port using Comm recv. I then need to work with the recieved data at a Bit level - ie. be able to read each individual bit value in the recieved string.

    Regards
    John

  • #2
    Check HELP for CVBYT, BIN$, BIT, etc.
    LOCAL MyEMail AS STRING
    MyEmail = STRREVERSE$("53pmohtj") & CHR$(64) & STRREVERSE$("liamg") & CHR$(46) & STRREVERSE$("moc")

    Comment


    • #3
      John,
      the following program shows one way to extract the bits for each character of a string.

      Paul.
      Code:
      'PBCC5.01 program
      FUNCTION PBMAIN()
      StringData$="ABCdef"
      
      FOR Char& = 1 TO LEN(StringData$)
          PRINT MID$(StringData$,Char&,1);
          Character? = CVBYT(StringData$,Char&)
          FOR WhichBit& = 7 TO 0 STEP -1
              PRINT BIT(Character?,WhichBit&);
          NEXT
          PRINT
      NEXT
      
      WAITKEY$
                
      END FUNCTION

      Comment


      • #4
        Serial Port but at a "Bit" level???
        Hmmmmmm, Interestinnnnnnng.......
        Engineer's Motto: If it aint broke take it apart and fix it

        "If at 1st you don't succeed... call it version 1.0"

        "Half of Programming is coding"....."The other 90% is DEBUGGING"

        "Document my code????" .... "WHYYY??? do you think they call it CODE? "

        Comment


        • #5
          Here is a function to read various bits or groups of bits in the string.

          Technical note: It does everything "big endian". That means the most significant bits start from the left. This holds true for the input string bytes as well. It can be converted quite easily to the reverse--"little endian"--if needed, where the most significant bits are on the right. It could even be split, for example, to a big endian output from a little endian input string.
          Code:
          #COMPILE EXE
          #DIM ALL
          
          FUNCTION showStringBits(stringIn AS STRING, BYVAL firstBit AS LONG, BYVAL lastBit AS LONG) AS STRING
             STATIC oneTime, ii, strLen AS LONG
             DIM bitTable(255) AS STATIC STRING * 8
             LOCAL bitConvertStr AS STRING
             
             IF oneTime = 0 THEN
                FOR ii = 0 TO 255                    'make an array one time only which shows binary version of all 256 bytes
                   bitTable(ii) = BIN$(ii, 8)
                NEXT
             END IF
             
             strLen = LEN(stringIn)
             
             'do a little range validity checking:
             IF firstBit > lastBit THEN SWAP firstBit, lastBit 'if first bit asked for is larger than last bit, swap them
             IF firstBit < 1 THEN firstBit = 1
             IF lastBit  < 1 THEN lastBit  = 1
             IF lastBit  > 8 * strLen THEN lastBit = 8 * strLen
             
             bitConvertStr = SPACE$(8 * strLen)      'our initial string to hold 1's and 0's
             
             FOR ii = 0 TO strLen - 1
                MID$(bitConvertStr, 8 * ii + 1) = bitTable(ASC(stringIn, ii + 1)) 'convert to 1's and 0's
             NEXT
             
             FUNCTION = MID$(bitConvertStr, firstBit, lastBit - firstBit + 1)     'choose bits to display
                
          END FUNCTION  
              
          FUNCTION PBMAIN () AS LONG
             LOCAL strIn, bitStr AS STRING, firstBit AS LONG
             
             strIn = CHR$(51,52,53,54,55,56,57,58,59)
             
             bitStr = showStringBits(strIn, 1, 33)
             ? bitStr & $CRLF & "123456789012345678901234567890123"
          
             bitStr = showStringBits(strIn, 11, 11)
             ? bitStr
             bitStr = showStringBits(strIn, 12, 12)
             ? bitStr
             bitStr = showStringBits(strIn, 13, 13)
             ? bitStr
             bitStr = showStringBits(strIn, 14, 14)
             ? bitStr
             
             firstBit = 7
             bitStr = showStringBits(strIn, 68, firstBit)
             ? STRING$(firstBit - 1, "$") & bitStr & $CRLF & "12345678901234567890123456789012345678901234567890123456789012345678"
          
             firstBit = 17
             bitStr = showStringBits(strIn, 68, firstBit)
             ? STRING$(firstBit - 1, "$") & bitStr & $CRLF & "12345678901234567890123456789012345678901234567890123456789012345678"
              
             bitStr = showStringBits(strIn, 1, 1338)
             ? bitStr & $CRLF & "123456789012345678901234567890123456789012345678901234567890123456789012"
          
          END FUNCTION

          Comment


          • #6
            Sometimes I need to 'deconstruct' data that has been squeezed into a few bytes of serial data. (Typically position reports sent from a mobile unit).
            I stuff all the 'bits' into an array and pick through that to get the individual values. Something like..
            Code:
             sTest = "SomeBytes"                              ' Raw data 
             Redim BitArray(Len(sTest)*8)                     ' BitArray() As String ("1"s and "0"'s)
             bp = StrPtr(sTest)                               ' bp as Byte Ptr
             
              For b = 0 To Len(sTest)-1                       ' one byte at a time
                TempChar = Bin$(@bp[b],8)                     ' TempChar => 'Binary' string
                For c = 1 To 8                                ' one bit at a time
                  BitArray(i) = Mid$(TempChar, c, 1)          ' build bit array
                  Incr i
                Next
              Next
             
              For i = 3 To 6                                  ' e.g. group of bits 
                sGroup += BitArray(i)
              Next
              ExampleRes? = Val("&B0"+sGroup)
            Rgds, Dave

            Comment

            Working...
            X