Results 1 to 4 of 4

Thread: [2005] Reading Bits in a Byte Array

  1. #1

    Thread Starter
    Addicted Member Daystar's Avatar
    Join Date
    Dec 2006
    Location
    Pahrump, NV
    Posts
    132

    Angry [2005] Reading Bits in a Byte Array

    Okay here is the problem, I want to read a Base64 Encoded Message. Which can be converted back to a Byte Array. But I need to read the bits in specific positions to retrieve the information. In VB.Net is this possible or does the program need to be written in C# or C++?

    I am using the following count to return the bit value

    Code:
      Public Function GetBits(byVal [Byte] as Byte, OffSet as Integer, Count as Integer) As Integer
       Return ([Byte] >> Offset) + ((1 << Count) - 1)
     End Function
    The following should return the bit count that needs to be read for the total bits used to encode the profession in the string.

    Code:
      bits_per_profession = code * 2 + 4
    Since they say it should be stored in the second byte since the first 8 bits are set aside for version information I am doing the following.

    Code:
      dim cBits as Integer = GetBits([ByteArray](2), 0, 2)
    It just seems that doesn't return anything useful.
    Last edited by Daystar; May 1st, 2007 at 06:24 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Reading Bits in a Byte Array

    To read a single bit you you can perform a logical AND operation:
    vb Code:
    1. Public Function GetBit(ByVal data As Byte, ByVal position As Integer) As Boolean
    2.     If position < 0 OrElse position > 7 Then
    3.         Throw New ArgumentException("Value must be within the range 0 to 7 inclusive.", "position")
    4.     End If
    5.  
    6.     Dim mask As Byte = CByte(2 ^ position)
    7.  
    8.     Return (data And mask) = mask
    9. End Function
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2005] Reading Bits in a Byte Array

    Bits_per_proffession = code >>1 +4

    You say that the byte you need is byte 2, but then you look at ByteArray(2). Since .NET is 0 based, that is the third byte in the array.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Addicted Member Daystar's Avatar
    Join Date
    Dec 2006
    Location
    Pahrump, NV
    Posts
    132

    Re: [2005] Reading Bits in a Byte Array

    Quote Originally Posted by Shaggy Hiker
    Bits_per_proffession = code >>1 +4

    You say that the byte you need is byte 2, but then you look at ByteArray(2). Since .NET is 0 based, that is the third byte in the array.
    I explained it wrong.

    Example of the data string
    Code:
    OwUUEOX9W4O1j7aExDKCs4yDACA
    Example from the older strings
    Code:
    A0MAxm24oCbD/h3Qmol0JEAA
    I am assuming it is Base64 Encoded Stream

    Code:
     Private mArr() as Byte
     Public Sub GetBytes(byVal [String] as String)
        mArr() = Convert.FromBase64String([String])
     End Sub
    
     Public Function DispVal(byVal [Byte] as Byte) as Integer
        Return GetBits([Byte], 2, 6)
     End Sub
    For some reason that always returns really funny numbers. Also the correct code was

    Code:
      bits_per_profession = code * 2 + 4
    The unknown is the code

    If I reversed that when reading those bits would it return the right value?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width