# Base Conversion Pro

The function BaseConv convert any base 10 numbers to any selected base (max 64) and the function ConvBase10 convert it back to base 10.
It can be useful for cryptography purpose example: convert the ASCII value of a char to a secret base...or it can be useful for any algorithm that returns numbers and that you want as a string. The maximum base could be extended by adding other characters to the constant DIGITS. With the original DIGITS this algorithm is compatible for conversion to common base: Binary (2), Hex (16), Octal (8).

Original Author: Vincent Bouret

### Inputs

Use: BaseConv(base10_original_number, newbase)
to convert in a particular base
Or use: ConvBase10(otherbase_number, oldbase)

### Assumptions

Juste paste the code anywhere.

### Returns

BaseConv returns a string and ConvBase10 return a long integer

### Side Effects

If you add char to DIGITS (to extend the base maximum) be aware to avoid adding the same char twice or else you will get an error.

### Code

`'Use: BaseConv(base10_original_number, newbase)'to convert in a particular base'Or use: ConvBase10(otherbase_number, oldbase)Const ZERO = "0"Const DIGITS = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/.="Function ConvBase(n1 As Double, base As Long) As StringDim e2(30) As DoubleDim n As IntegerDim i As LongDim max As LongIf base > Len(DIGITS) Then ConvBase = "NULL": Exit FunctionIf n1 = 1 Then ConvBase = "1": Exit Functionn = 0Do While (base ^ n) <= n1 n = n + 1Loopn = n - 1i = 0Do While n > -1 e2(i) = n1 (base ^ n) n1 = n1 Mod (base ^ n) n = n - 1 i = i + 1Loopmax = i - 1For i = 0 To max If e2(i) = 0 Then  ConvBase = ConvBase & ZERO Else  ConvBase = ConvBase & Mid(DIGITS, e2(i), 1) End IfNext iEnd FunctionFunction ConvBase10(num As String, base As Long) As DoubleDim i As LongDim n As Longn = Len(num)For i = 1 To n If Mid(num, i, 1) <> ZERO Then ConvBase10 = ConvBase10 + (InStr(1, DIGITS, Mid(num, i, 1)) * (base ^ (n - i))) End IfNext iEnd Function`

Posted: 2002-06-01
By: ArchiveBot
Viewed: 137 times

Categories

Visual Basic 6

Attachments

No attachments for this post