ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   VBA - Base 34 Conversion (https://www.excelbanter.com/excel-programming/339130-vba-base-34-conversion.html)

ajocius[_28_]

VBA - Base 34 Conversion
 

Group,
Can someone show me how I can use VBA to make a Base 34
conversion?

Tony


--
ajocius
------------------------------------------------------------------------
ajocius's Profile: http://www.excelforum.com/member.php...o&userid=17695
View this thread: http://www.excelforum.com/showthread...hreadid=401691


Jim Rech

VBA - Base 34 Conversion
 
I needed some big base routines a few years ago and came up with these.
While I'm sure real math heads could do it more efficiently, fwiw:

Dim CharArray() As String

Sub Test()
''Converts 30 to base 34
MsgBox Base10ToX(30, 34)
End Sub

Sub TestBackToTen()
''Converts U (base 34) to base 10
MsgBox BaseXTo10("U", 34)
End Sub

'Supports bases up to 36
Function Base10ToX(Number As Long, NewBase As Integer) As String
Dim Balance As Long, Remainder As Long
If MakeCharArray(NewBase) Then
Balance = Abs(Number)
Do
Remainder = Balance Mod NewBase
Base10ToX = CharArray(Remainder) & Base10ToX
Balance = (Balance - Remainder) / NewBase
Loop While Balance 0
If Number < 0 Then Base10ToX = "-" & Base10ToX
Else
Base10ToX = "error"
End If
End Function

Function BaseXTo10(NumStr As String, NewBase As Integer) As Long
Dim Counter As Integer, IsNeg As Boolean
Dim CurrChar As String, NumStrLen As Integer
Dim CurrDigitValue As Integer, ResultNum As Double
If MakeCharArray(NewBase) Then
If Left(NumStr, 1) = "-" Then
NumStr = Trim(Mid(NumStr, 2, 99))
IsNeg = True
End If
NumStrLen = Len(NumStr)
For Counter = 1 To NumStrLen
CurrChar = Mid(NumStr, NumStrLen - Counter + 1, 1)
CurrDigitValue = Application.Match(CurrChar, CharArray, 0) - 1
ResultNum = ResultNum + CurrDigitValue * NewBase ^ (Counter - 1)
Next
If IsNeg Then ResultNum = -ResultNum
BaseXTo10 = ResultNum
Else
BaseXTo10 = 0
End If
End Function

Function MakeCharArray(Base As Integer) As Boolean
Dim Counter As Integer, TempInt As Integer
If Base 1 And Base < 37 Then
ReDim CharArray(0 To Base - 1)
For Counter = 0 To Base - 1
TempInt = Counter + 48
If TempInt 57 Then TempInt = TempInt + 7
CharArray(Counter) = Chr(TempInt)
Next
MakeCharArray = True
End If
End Function

--
Jim
"ajocius" wrote in
message ...

Group,
Can someone show me how I can use VBA to make a Base 34
conversion?

Tony


--
ajocius
------------------------------------------------------------------------
ajocius's Profile:
http://www.excelforum.com/member.php...o&userid=17695
View this thread: http://www.excelforum.com/showthread...hreadid=401691




Tushar Mehta

VBA - Base 34 Conversion
 
Search the google.com archives of the XL newsgroups. You will find any
number of solutions including this one:
http://groups.google.com/group/micro...c/browse_frm/t
hread/5923bd418ffe3207/b3cd2d0f7f2bb5e5

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...

Group,
Can someone show me how I can use VBA to make a Base 34
conversion?

Tony


--
ajocius
------------------------------------------------------------------------
ajocius's Profile:
http://www.excelforum.com/member.php...o&userid=17695
View this thread: http://www.excelforum.com/showthread...hreadid=401691




All times are GMT +1. The time now is 06:13 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com