Solved

Pulling out '0' and '1' from a byte

Posted on 1998-10-18
4
153 Views
Last Modified: 2010-04-30
Is there a easy way of telling what bit's are set in a byte?
Can someone please give me an example of a function that takes a byte (0-255) as input and leaves perhaps a string containing 8 chars i.e "00101101" as a result?
I need to test if certain bit's are set, for example value 4 (the third from the right) ignoring other bit's that are set.
0
Comment
Question by:rakkas
4 Comments
 
LVL 2

Accepted Solution

by:
schild earned 100 total points
ID: 1440510
try this, its working


Private Function ByteToBool(ByVal ByteVal As Byte) As String

    Dim Index As Integer
   
    For Index = 1 To 8
        ByteToBool = IIf(ByteVal And 1, "1", "0") & ByteToBool
        ByteVal = ByteVal / 2
    Next
   
End Function


Good Luck
Schild
0
 
LVL 2

Expert Comment

by:Poddy
ID: 1440511
I would recommed using the And operator.

To test for bits being set, take the decimal value of the bit, and AND it against the tested value.

If it is set it will return the value that you are testing for, if not, it will return zero

Bracket the AND expression or you may get strange results (AND has lower priority than =)

So

(7 and 4)  = 4
(8 and 4)  = 0

etc



0
 
LVL 1

Author Comment

by:rakkas
ID: 1440512
Thanks, Poddy and schild.
You saved my day!!!
0
 

Expert Comment

by:namiesiao
ID: 5414596
The above doesnt seems to work with some numbers. Take 100 for example. I've modified it a bit into the following code. Thanks for your code though, schild.

Public Function ByteToBin(ByVal ByteVal As Byte) As String
Dim Index As Integer

For Index = 1 To 8
    ByteToBin = IIf(ByteVal Mod 2, "1", "0") & ByteToBin
    ByteVal = ByteVal \ 2
Next
End Function
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question