Results 1 to 2 of 2

Thread: VB - Convert Decimal Values to Various Bases

  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.

  2. #2
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    Here's how to convert any base to any other base:

    VB Code:
    1. Public Function ChangeBase(iNumber As Double, iFromBase As Long, iToBase As Long) As Double
    2. On Error GoTo 1
    3.  
    4.     Dim A As Long
    5.     Dim Temp As String
    6.     Dim Length As Long
    7.    
    8.     Dim Number As Long
    9.     Dim Base10 As Long
    10.     Dim Result As Long
    11.    
    12.     Dim Minus As Long
    13.    
    14.     Dim Floating As Double
    15.    
    16.     'Get floating part
    17.     Floating = iNumber - Int(iNumber)
    18.     If Floating > 0 Then: Floating = iToBase / (iFromBase / Floating)
    19.    
    20.     'Pre-check base
    21.     Minus = Sgn(iNumber)
    22.     If iFromBase = 10 Then
    23.         Base10 = Abs(CLng(iNumber))
    24.    
    25.     Else
    26.         'Get number string
    27.         Temp = CStr(Abs(CLng(iNumber)))
    28.         Length = Len(Temp)
    29.        
    30.         'Convert to 10-base
    31.         For A = 0 To Length - 1
    32.             'Check symbol
    33.             Number = CLng(Mid(Temp, Length - A, 1))
    34.            
    35.             'Add to result
    36.             Base10 = Base10 + (iFromBase ^ A) * Number
    37.         Next
    38.     End If
    39.    
    40.     'Pre-check base
    41.     If iToBase = 10 Then
    42.         ChangeBase = Minus * (CDbl(Base10) + Floating)
    43.        
    44.         Exit Function
    45.     End If
    46.    
    47.     'Convert to new base
    48.     Length = 0
    49.     While Base10 > 0
    50.         If Base10 < iToBase Then
    51.             'Last part
    52.             Result = Result + (10 ^ Length) * Base10
    53.             Base10 = 0
    54.            
    55.         Else
    56.             'Get value
    57.             Number = Base10 Mod iToBase
    58.             Result = Result + (10 ^ Length) * Number
    59.             Base10 = Int(Base10 / iToBase)
    60.         End If
    61.        
    62.         'Next 10-factor
    63.         Length = Length + 1
    64.     Wend
    65.    
    66.     'Return result
    67.     ChangeBase = Minus * (CDbl(Result) + Floating)
    68.     Exit Function
    69.  
    70. 1  'Error
    71. End Function
    [edit]Added support for negative and floating point numbers[/edit]
    Last edited by Fox; Jun 23rd, 2003 at 07:30 AM.

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