# converting between differnt number systems(bases)

Page 1 of 1

## 1 Replies - 5592 Views - Last Post: 26 April 2010 - 09:33 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=170356&amp;s=74a94abf1e5156455aa24c0c5e7807e8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jakerman999

Reputation: 1
• Posts: 17
• Joined: 07-August 08

# converting between differnt number systems(bases)

Posted 26 April 2010 - 09:30 AM

Hello. I am Working on an encryption program that takes the numerical value of a string, and changes the base of it so that the string cannot be properly read. for this, I am currently using a module that contains two versions of the same function, switching in and out of base ten(the decimal system). I picked up the functions from This website and am trying to understand how they work so I can create streamlined versions for my program, but they return the error:

"Run Time Error '424';
Object required

and highlight the line:

```myIndex = Application.WorksheetFunction.Match(myStr, Digits) - 1
```

Here is the module I am currently using.

```
Public str16 As String
Public str10 As String
Public str32 As String

Public Function Dec2Base(Num As Long, base As Long) As String
'converts a decimal number to the equivalent in the specified base
'(base 2 to base 16).  Base needs to be specified as decimal ie
'8 for base 8, 16 for base 16, 2 for base 2 etc
Static Digits As Variant
Dim i As Long, alHolder() As Long, sTemp As String

If IsEmpty(Digits) Then Digits = VBA.Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9")

'check valid base:
If base > 32 Then Dec2Base = "Invalid base used": Exit Function
'fill holder array:
i = 0
Do
ReDim Preserve alHolder(0 To i)
alHolder(i) = Num Mod base
i = i + 1
Num = Num \ base
Loop While Num > 0

'build string result in base:
sTemp = ""
For i = i - 1 To 0 Step -1
sTemp = sTemp & Digits(alHolder(i))
Next i
'output:
Dec2Base = sTemp
End Function

Public Function Base2Dec(Num As String, base As Long) As Variant
'converts a number in the specified base
'(base 2 to base 32) into the equivalent decimal.
'8 for base 8, 16 for base 16, 2 for base 2 etc
Static Digits As Variant
Dim i As Integer
Dim myIndex As Integer
Dim myStr As String

If IsEmpty(Digits) Then Digits = VBA.Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9")

'check valid base:
If base > 32 Then Base2Dec = "Invalid base used": Exit Function
'fill holder array:
Base2Dec = 0

For i = Len(Num) To 1 Step -1
myStr = Mid(Num, i, 1)
myIndex = Application.WorksheetFunction.Match(myStr, Digits) - 1
Base2Dec = Base2Dec + myIndex * base ^ (Len(Num) - i)
Next i

End Function

```

All help is Appreciated, thanks in advance.

This post has been edited by jakerman999: 26 April 2010 - 09:37 AM

Is This A Good Question/Topic? 0

## Replies To: converting between differnt number systems(bases)

### #2 jakerman999

Reputation: 1
• Posts: 17
• Joined: 07-August 08

## Re: converting between differnt number systems(bases)

Posted 26 April 2010 - 09:33 AM

how do I delete double posts?

This post has been edited by jakerman999: 26 April 2010 - 09:39 AM