Results 1 to 2 of 2

Thread: VB - Convert Decimal Values to Various Bases

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2002
    Posts
    9

    VB - Convert Decimal Values to Various Bases

    This is a function I wrote a while back. There are many ways to convert decimal to binary, and several have already been posted, but I really like this algorithm that I wrote. I was trying to go for a KISS approach here, but feel free to make it more professional.

    VB Code:
    1. Public Function ChangeBase(N As Long, NewBase As Integer) As String
    2.  
    3. Dim K As Long
    4. Dim L As Long
    5.  
    6.     Do
    7.         K = N \ NewBase
    8.         L = N - (K * NewBase)
    9.         N = K
    10.         ChangeBase = L & ChangeBase
    11.         DoEvents
    12.     Loop Until K < NewBase
    13.     ChangeBase = K & ChangeBase
    14.  
    15. End Function

    This will convert a decimal value to base 2, 3, 4, 5... whatever. However, if you want to convert to a base above base 10, you'll have to write some sort of code to catch values above 10 and convert them to a letter in the alphabet.
    Last edited by Tito; May 24th, 2003 at 02:43 PM.

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