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.
LVL 10
smeggheadAsked:
Who is Participating?
 
caraf_gConnect With a Mentor Commented:
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
 
deightonCommented:
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
 
deightonCommented:
Example call

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

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

i.e. base 10 to 2.


0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
caraf_gCommented:
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
 
smeggheadAuthor Commented:
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
 
smeggheadAuthor Commented:
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
 
smeggheadAuthor Commented:
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
 
caraf_gCommented:
Thanks smegghead... Actually, sounds like a good idea for a suggestion!
0
 
deightonCommented:
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
 
deightonCommented:
Ripped-off :-(
0
 
smeggheadAuthor Commented:
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
 
deightonCommented:
ok I didn't think of that scenario.
0
All Courses

From novice to tech pro — start learning today.