?
Solved

Binary strings...

Posted on 1999-07-19
12
Medium Priority
?
297 Views
Last Modified: 2010-04-30
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
Comment
Question by:smegghead
[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
  • Learn & ask questions
  • 5
  • 4
  • 3
12 Comments
 
LVL 18

Expert Comment

by:deighton
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

by:deighton
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

by:caraf_g
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Author Comment

by:smegghead
ID: 1525760
Thanks for your efforts, but...

"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

by:smegghead
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

by:
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

by:smegghead
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.

As to the grade, I'd never give anything less than 'Excellent', unless the answer didn't fully answer my question... which your did.

Smeggy
0
 
LVL 10

Expert Comment

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

Expert Comment

by:deighton
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

by:deighton
ID: 1525766
Ripped-off :-(
0
 
LVL 10

Author Comment

by:smegghead
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

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

719 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