Results 1 to 3 of 3

Thread: VB - Convert Decimal Values To Binary Values

  1. #1

    Thread Starter
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    VB - Convert Decimal Values To Binary Values

    I fixed it a bit...
    Attached Files Attached Files
    Last edited by manavo11; Apr 5th, 2003 at 11:09 AM.


    Has someone helped you? Then you can Rate their helpful post.

  2. #2
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: VB - Convert Decimal Values To Binary Values

    This is a 5 year old post, but how about this very short function:
    Code:
    Function Dec2Bin(ByVal Num As Long) As String
       Select Case Num
          Case Is > 1: Dec2Bin = Dec2Bin(Num \ 2) & (Num Mod 2)
          Case 0, 1:   Dec2Bin = Num
          Case Else:   Dec2Bin = -Dec2Bin(-Num)
       End Select
    End Function
    This perhaps better matching with Hex and Oct, that use a feature of Oct() with 32-bit 2's complement for negative numbers:
    Code:
    Function Bin(ByVal Num As Long) As String
       Dim O2B As Variant, sOct As String, i As Long
       
       If Num = 0 Then Bin = "0": Exit Function
       O2B = Array("000", "001", "010", "011", "100", "101", "110", "111")
       sOct = Oct$(Num)
       Bin = Space$(3 * Len(sOct))
       For i = 1 To Len(sOct)
          Mid$(Bin, (i - 1) * 3 + 1, 3) = O2B(Mid$(sOct, i, 1))
       Next
       Bin = Mid$(Bin, InStr(Bin, "1"))
    End Function
    Last edited by anhn; Sep 5th, 2008 at 05:43 AM.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VB - Convert Decimal Values To Binary Values

    here is another method that may not be as good, but is pretty simple and not recursive if that is an advantage
    Code:
    Function binn(dnum As Long) As String
    If dnum = 0 Then binn = 0: Exit Function
    Dim bnum As String
    i = 0
    Do While dnum \ 2 ^ i >= 1
        bnum = Abs((dnum And 2 ^ i) = 2 ^ i) & bnum
        i = i + 1
    Loop
    binn = bnum
    End Function
    Last edited by westconn1; Sep 5th, 2008 at 06:07 PM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width