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
Printable View
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
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?
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
to replace the LSB with another value you can simply do this:VB Code:
MyByte = (MyByte And &HFE) Or NewLSBValue
To replace the LSB (assuming it is the bit with a value of 1) you can use fairly simple code:
...or more efficiently:VB Code:
Function SetLSB (p_lngValue as Long, p_lngBit as Long) as Long If p_lngBit = 1 Then If p_lngValue Mod 2 = 0 Then SetLSB = p_lngValue + 1 Else SetLSB = p_lngValue End If Else If p_lngValue Mod 2 = 0 Then SetLSB = p_lngValue Else SetLSB = p_lngValue - 1 End If End If End Function
.. or of course you could use moeur's simpler version :lol:VB Code:
Function SetLSB (p_lngValue as Long, p_lngBit as Long) as Long If p_lngBit Then SetLSB = p_lngValue + (1 - (p_lngValue Mod 2)) Else SetLSB = p_lngValue - (p_lngValue Mod 2) End If End Function