Solved

Binary strings...

Posted on 1999-07-19
Medium Priority
297 Views
How can I convert simply from a base 10 number into a binary string and vice versa... is there an equivalent of &hF0 for binary ??? &b10101 doesn't work, but would be nice - I don't want to go down the user-function to do this, I want a nice simple solution... maybe asking too much.
0
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 5
• 4
• 3

LVL 18

Expert Comment

ID: 1525757
Private Function nTOm(sX As String, n As Integer, m As Integer)

'works for number bases 2 to 16

Dim lNumber As Long
Dim c As Integer
Dim sAtom As String
Dim lVal As Long
Dim x As Long

For c = Len(sX) To 1 Step -1

sAtom = UCase(Mid(sX, c, 1))

If n > 10 Then

If sAtom >= "A" And sAtom <= "F" Then

lVal = Asc(sAtom) - Asc("A") + 10

Else

lVal = CLng(sAtom)

End If

Else

lVal = CLng(sAtom)

End If

lNumber = lNumber + lVal * n ^ x

x = x + 1

Next

nTOm = ""

While lNumber > 0

x = lNumber Mod m

If x < 10 Then

sAtom = CStr(x)

Else

sAtom = Chr(Asc("A") + x - 10)

End If

nTOm = sAtom + nTOm

lNumber = lNumber \ m

Wend

If nTOm = "" Then nTOm = "0"

End Function

0

LVL 18

Expert Comment

ID: 1525758
Example call

Text2 = nTOm("17", 10, 2)

changes decimal 17 (as a string) to a binary string

i.e. base 10 to 2.

0

LVL 10

Expert Comment

ID: 1525759
Even easier

1 - use the Hex function to convert from a number to a hexadecimal string
2 - convert each character as follows:

0: 0000
1: 0001
...
F: 1111

0

LVL 10

Author Comment

ID: 1525760

"quote"
I don't want to go down the user-function to do this, I want a nice simple solution
"quote"

Sorry
0

LVL 10

Author Comment

ID: 1525761
Cafaf_g - yeah, that's what I've done as an interim solution, I just want to be able to convert using a built-in function.
0

LVL 10

Accepted Solution

caraf_g earned 200 total points
ID: 1525762
There isn't a built-in function.

I know this is not the answer you want to hear, but it is the correct answer. Still, if you don't like it, I would prefer it if you would reject it, rather than accept it with a bad grade.

Thanks

Pino
0

LVL 10

Author Comment

ID: 1525763
you're right (I hope) it is the right answer, although I do believe that EE should add another category to the 'Accept' options to say 'Accept with negative response' to prevent people from spending their points on PAQ's that don't give a solution.

Smeggy
0

LVL 10

Expert Comment

ID: 1525764
Thanks smegghead... Actually, sounds like a good idea for a suggestion!
0

LVL 18

Expert Comment

ID: 1525765
What was wrong with my function?

If you work with your own function then why is it different to using a built in function?  If you had a function to do everthing then you wouldn't need a programmer.
0

LVL 18

Expert Comment

ID: 1525766
Ripped-off :-(
0

LVL 10

Author Comment

ID: 1525767
I'm sure there is nothing wrong with your function, but it was exactly what I wanted to avoid having to do.

It was more a matter of interest than finding a solution. I was just in 'debug' mode and wanted to find the binary equivalent of a number, which I just worked out on paper. I just thought {[there has to be an easier way]}

Sorry if you feel ripped off, but I did say in my question that I didn't want to have a user-written function to do this...

:-)

0

LVL 18

Expert Comment

ID: 1525768
ok I didn't think of that scenario.
0

Featured Post

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applicâ€¦
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â€¦
Suggested Courses
Course of the Month10 days, 17 hours left to enroll