Solved

String-Bin-Dec

Posted on 2000-03-13
3
421 Views
Last Modified: 2012-06-21
I have a String = 11001101 or CD

How can I ...
i) cast the string to Bin/Hex?
ii) get the value of Dec for the Bin and Hex?

TQ;
0
Comment
Question by:pkimk
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
Tchalkov earned 10 total points
ID: 2611602
Private Function BinToDec(str As String) As String
    If Len(str) = 0 Then
        BinToDec = 0
        Exit Function
    End If
    Dim i As Integer
    Dim d As Long
    Dim l As Integer
    Dim rez As Long
    l = Len(str)
    d = 1
    For i = 1 To l
        If Mid(str, l + 1 - i, 1) = "1" Then
            Res = Res + d
        End If
        d = d * 2
    Next i
    BinToDec = CStr(Res)
End Function
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2612012
Bin is a bit more work, but from Hex to Dec and vice versa is easy.

strHex = Hex(strDec)
strDec = cstr(val("&H" & strhex))

For Hex to Bin, make sure that len(strBin) is a multiple of four

For lngCounter = 1 to len(strBin) Step 4
    strDigit = Mid(strBin, lngCounter, 4)

Then translate strDigit according to the following table:

0000 - 0
0001 - 1
0010 - 2
....
1001 - 9
1010 - A
....
1111 - F

So, that allows you to convert a binary string to a hexadecimal. To convert to decimal, I suggest you convert to hex first.

If a binary string has a number of digits that is not a multiple of four, prefix with "0" until it is.

Hope this helps.
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2612015
"For Hex to Bin, make sure that "

Oops, that should have been

"For Bin to Hex, make sure that "

Hex to Bin is easy, just process one character at a time and replace with four-character combination as per above table.

Dec to Bin, again, via Hex
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now