Results 1 to 5 of 5

Thread: converting bytes to bits

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2005
    Posts
    19

    converting bytes to bits

    how do you convert a byte of data to its corresponding bit value.
    Is there any API already or do we have to use some bitwise operations....

    Thank you

  2. #2
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: converting bytes to bits

    you have to do bitwise operations.
    Exactly what are you trying to do?
    take some action if a bit is set?
    trying to set some bits?
    get a string representation of the bits?

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2005
    Posts
    19

    Re: converting bytes to bits

    i was just surfing the net and i got the required code.
    am tring the get the bit value for its corresponding byte and replace the least significant bit with some other bit value.
    It takes in long values and returns string representatin of bits.
    The code is as follows.

    ------------------------------

    Private Function LongToBinary(ByVal long_value As Long, _
    Optional ByVal separate_bytes As Boolean = True) As _
    String
    Dim hex_string As String
    Dim digit_num As Integer
    Dim digit_value As Integer
    Dim nibble_string As String
    Dim result_string As String
    Dim factor As Integer
    Dim bit As Integer

    ' Convert into hex.
    hex_string = Hex$(long_value)


    ' Zero-pad to a full 8 characters.
    hex_string = Right$(String$(8, "0") & hex_string, 8)

    Debug.Print (hex_string)


    ' Read the hexadecimal digits
    ' one at a time from right to left.
    For digit_num = 8 To 1 Step -1
    ' Convert this hexadecimal digit into a
    ' binary nibble.
    digit_value = CLng("&H" & Mid$(hex_string, _
    digit_num, 1))
    Debug.Print ("&H" & Mid$(hex_string, digit_num, 1))



    ' Convert the value into bits.
    factor = 1
    nibble_string = ""
    For bit = 3 To 0 Step -1
    If digit_value And factor Then
    nibble_string = "1" & nibble_string
    Debug.Print ("nibble_sting = " & nibble_string)
    Else
    nibble_string = "0" & nibble_string
    Debug.Print ("nibble_sting = " & nibble_string)
    End If
    factor = factor * 2
    Debug.Print ("factor = " & factor)
    Next bit

    ' Add the nibble's string to the left of the
    ' result string.
    result_string = nibble_string & result_string
    Debug.Print ("result_string = " & result_string)
    Next digit_num

    ' Add spaces between bytes if desired.
    If separate_bytes Then
    result_string = _
    Mid$(result_string, 1, 8) & " " & _
    Mid$(result_string, 9, 8) & " " & _
    Mid$(result_string, 17, 8) & " " & _
    Mid$(result_string, 25, 8)
    End If

    ' Return the result.
    LongToBinary = result_string
    End Function

  4. #4
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: converting bytes to bits

    to replace the LSB with another value you can simply do this:
    VB Code:
    1. MyByte = (MyByte And &HFE) Or NewLSBValue

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: converting bytes to bits

    To replace the LSB (assuming it is the bit with a value of 1) you can use fairly simple code:
    VB Code:
    1. Function SetLSB (p_lngValue as Long, p_lngBit as Long) as Long
    2.  
    3.   If p_lngBit = 1 Then
    4.     If p_lngValue Mod 2 = 0 Then
    5.       SetLSB = p_lngValue + 1
    6.     Else
    7.       SetLSB = p_lngValue
    8.     End If
    9.   Else
    10.     If p_lngValue Mod 2 = 0 Then
    11.       SetLSB = p_lngValue
    12.     Else
    13.       SetLSB = p_lngValue - 1
    14.     End If
    15.   End If
    16.  
    17. End Function
    ...or more efficiently:
    VB Code:
    1. Function SetLSB (p_lngValue as Long, p_lngBit as Long) as Long
    2.  
    3.   If p_lngBit Then
    4.     SetLSB = p_lngValue + (1 - (p_lngValue Mod 2))
    5.   Else
    6.     SetLSB = p_lngValue - (p_lngValue Mod 2)
    7.   End If
    8.  
    9. End Function
    .. or of course you could use moeur's simpler version

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