Link to home
Start Free TrialLog in
Avatar of smegghead
smeggheadFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Binary strings...

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.
Avatar of deighton
deighton
Flag of United Kingdom of Great Britain and Northern Ireland image

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


Example call

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

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

i.e. base 10 to 2.


Avatar of caraf_g
caraf_g

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


Avatar of smegghead

ASKER

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
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.
ASKER CERTIFIED SOLUTION
Avatar of caraf_g
caraf_g

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Thanks smegghead... Actually, sounds like a good idea for a suggestion!
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.
Ripped-off :-(
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...

:-)

ok I didn't think of that scenario.