Solved

# String-Bin-Dec

Posted on 2000-03-13
419 Views
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
Question by:pkimk
• 2

LVL 2

Accepted Solution

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

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

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…