article

Numerical String and Long Conversion

Email
Submitted on: 3/3/2020 3:41:36 AM
By: Nicholas Forystek  
Level: Advanced
User Rating: Unrated
Compatibility: VB 6.0
Views: 1754
author picture
 
     Learning permit piece that converts a string numeric to a long and vice versa. I have included two versions because I can't decide which is best, perhaps the option on compile optimizations tells what would be better for you, or that is just too much to worry about for a irrelevant piece that is more of a "how to" in explore. For instance the select statements below are totally over rated in use of the VB environment, but if you express it from a assembly perspective. Having nothing but visual need on the screen and arithmetic, a case statement of at most 9 is' not so bad to cover a long where technically you could be like meet demand and half it for quick as if there will only be a byte and so have a long select statement that just case for cases numbers 0 to 255 and no logic expect that.

 
				'####################################################
'############### FASTCODE VERSION ###################
'####### uses extra variable J and calls less #######
Public Function Numerics(ByVal num As Variant) As Variant
 Dim i As Long
 Dim mul As Long
 If TypeName(num) = "String" Then
Numerics = CLng(0)
For i = 1 To Len(num)
 mul = 10 ^ (Len(num) - i)
 Select Case Mid(num, i, 1)
Case "1"
 Numerics = Numerics + (mul * 1)
Case "2"
 Numerics = Numerics + (mul * 2)
Case "3"
 Numerics = Numerics + (mul * 3)
Case "4"
 Numerics = Numerics + (mul * 4)
Case "5"
 Numerics = Numerics + (mul * 5)
Case "6"
 Numerics = Numerics + (mul * 6)
Case "7"
 Numerics = Numerics + (mul * 7)
Case "8"
 Numerics = Numerics + (mul * 8)
Case "9"
 Numerics = Numerics + (mul * 9)
 End Select
Next
Numerics = Numerics
 Else
Numerics = ""
mul = 1
Do Until ((num / mul) - (num \ mul)) = (num / mul)
 mul = mul * 10
 i = i + 1
Loop
Dim j As Long
j = i
'Numerics = String(i, " ")
Do
 mul = mul \ 10
 i = 9
 Do
mul = ((10 ^ j) * i)
i = i - 1
 Loop Until ((num \ mul) = 1) Or (i = 0)
 If (num \ mul) = 1 Then
Select Case (mul \ (10 ^ j))
 Case 1
Numerics = Numerics & "1"
 Case 2
Numerics = Numerics & "2"
 Case 3
Numerics = Numerics & "3"
 Case 4
Numerics = Numerics & "4"
 Case 5
Numerics = Numerics & "5"
 Case 6
Numerics = Numerics & "6"
 Case 7
Numerics = Numerics & "7"
 Case 8
Numerics = Numerics & "8"
 Case 9
Numerics = Numerics & "9"
End Select
num = num - mul
If j > 0 >= 1 Then
 mul = mul / (mul \ j)
End If
 Else
Numerics = Numerics & "0"
 End If
 j = j - 1
Loop Until j = -1
Numerics = Numerics
 End If
End Function
'####################################################
'################ SMALL CODE VERSION ################
'### No extra j variable but extra len/teim calls ###
Public Function Numerics(ByVal num As Variant) As Variant
 Dim i As Long
 Dim mul As Long
 If TypeName(num) = "String" Then
Numerics = CLng(0)
For i = 1 To Len(num)
 mul = 10 ^ (Len(num) - i)
 Select Case Mid(num, i, 1)
Case "1"
 Numerics = Numerics + (mul * 1)
Case "2"
 Numerics = Numerics + (mul * 2)
Case "3"
 Numerics = Numerics + (mul * 3)
Case "4"
 Numerics = Numerics + (mul * 4)
Case "5"
 Numerics = Numerics + (mul * 5)
Case "6"
 Numerics = Numerics + (mul * 6)
Case "7"
 Numerics = Numerics + (mul * 7)
Case "8"
 Numerics = Numerics + (mul * 8)
Case "9"
 Numerics = Numerics + (mul * 9)
 End Select
Next
Numerics = Numerics
 Else
Numerics = ""
mul = 1
Do Until ((num / mul) - (num \ mul)) = (num / mul)
 mul = mul * 10
 i = i + 1
Loop
Numerics = String(i, " ")
Do
 mul = mul \ 10
 i = 9
 Do
mul = ((10 ^ (((Len(Numerics) - 1) - (Len(Trim(Numerics)) + 1)) + 1)) * i)
i = i - 1
 Loop Until ((num \ mul) = 1) Or (i = 0)
 If (num \ mul) = 1 Then
Select Case (mul \ (10 ^ (((Len(Numerics) - 1) - (Len(Trim(Numerics)) + 1)) + 1)))
 Case 1
Numerics = Mid(Numerics, 2) & "1"
 Case 2
Numerics = Mid(Numerics, 2) & "2"
 Case 3
Numerics = Mid(Numerics, 2) & "3"
 Case 4
Numerics = Mid(Numerics, 2) & "4"
 Case 5
Numerics = Mid(Numerics, 2) & "5"
 Case 6
Numerics = Mid(Numerics, 2) & "6"
 Case 7
Numerics = Mid(Numerics, 2) & "7"
 Case 8
Numerics = Mid(Numerics, 2) & "8"
 Case 9
Numerics = Mid(Numerics, 2) & "9"
End Select
num = num - mul
If (10 ^ (((Len(Numerics) - 1) - (Len(Trim(Numerics)) + 1)) + 1)) > 0 >= 1 Then
 mul = mul / (mul \ (((Len(Numerics) - 1) - (Len(Trim(Numerics)) + 1)) + 1))
End If
 Else
Numerics = Mid(Numerics, 2) & "0"
 End If
Loop Until InStr(Numerics, " ") = 0
Numerics = Numerics
 End If
End Function


Other 33 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Advanced category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.