I have a sample of a code to convert numbers into N-base numbers.
I can have 2, 3 or 4 digits numbers.
My function works and look like this :
Private Function BaseN(s As String)
Dim d = Val(s(s.Length - 1))
Dim c = Val(s(s.Length - 2))
Dim a, b As Integer
Try
b = Val(s(s.Length - 3))
Catch
b = 0
End Try
Try
a = Val(s(s.Length - 4))
Catch
a = 0
End Try
Return (a * (n * n * n) + b * (n * n) + c * n + d + 1).ToString
End Function
But for a reason, like that it takes like 10 sec to convert my text.
I tried that way :
Private Function BaseN(s As String)
Dim d = Val(s(s.Length - 1))
Dim c = Val(s(s.Length - 2))
Dim a, b As Integer
b = 0
a = 0
Return (a * (n * n * n) + b * (n * n) + c * n + d + 1).ToString
End Function
And here the conversion is roughly instant.
So I was wondering why the try catch
were so long to run, or if I've made a bad usage of them.