Solved

String-Bin-Dec

Posted on 2000-03-13
3
424 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

777 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