|
1 members (Russ),
10,950
guests, and
1,516
robots. |
|
Key:
Admin,
Global Mod,
Mod
|
|
|
S |
M |
T |
W |
T |
F |
S |
|
|
|
1
|
2
|
3
|
4
|
5
|
|
6
|
7
|
8
|
9
|
10
|
11
|
12
|
|
13
|
14
|
15
|
16
|
17
|
18
|
19
|
|
20
|
21
|
22
|
23
|
24
|
25
|
26
|
|
27
|
28
|
29
|
30
|
31
|
|
|
|
|
This is another custom island. This one could be used for ads. This spot is one of the best places for ads on your site to be placed. This can be enabled/disabled on a per forum basis.
|
|
|
|
Joined: Dec 1999
Posts: 30,868
Master Elite Member
|
Public Function AsLong_ByteArray_ASCII(ByteArray() As Byte, Optional Default As Variant) As Long
'Purpose
' Used to convert an ASCII byte array to a Long.
'Parameters
' ByteArray (Byte()) = The byte array to convert.
' Default (Optional Variant Byte/Integer/Long) = The value to return if
' the specified ASCII byte array is not convertable to a Long.
' If this value is not a Byte or Integer or Long, it defaults to zero.
' Default value: 0 (zero)
'Return (Long)
' Returns the specified byte array as an Integer.
' If the specified byte array contains a decimal value, it is disregarded (no rounding takes place).
' Function returns zero if:
' - the specified byte array is empty, or
' - the first character of the specified byte array is a decimal point (".123"), or
' - the first two character of the specified byte array are a minus sign and a decimal point ("-.123"), or
' - the first two character of the specified byte array are a plus sign and a decimal point ("+.123"), or
' - all characters between a leading minus sign and a decimal point are zeros (-000.123), or
' - all characters between a leading plus sign and a decimal point are zeros (+000.123).
' Function returns the specified default value if:
' - the value contains any characters other than "0123456789.+-" (whether before or after the decimal), or
' - the value is less than -2,147,483,648, or
' - the value is greater than 2,147,483,647, or
' - the value contains more than one decimal point, or
' - the value contains a minus sign (-) in any position other than the beginning, or
' - the value contains a plus sign (+) in any position other than the beginning.
' Additional considerations
' There can be any number of leading zeros.
' They are all ignored and will not affect the returned value.
' Examples: 0000000000000123 = 123
' -00000000000000000000000123.456 = -123
' There can be any number of digits after the decimal.
' They are all ignored and will not affect the returned value.
' Example: 123.983983897438778432764328723487 = 123
' -123.000000000000000000000000000000000000005 = 123
'Dependencies
' ElementCount()
'Notes
' The specified byte array stores a string of ASCII characters representing a Long.
' This is an example of an "ASCII" Long with a value of 12:
' (0) 48 "0" (1) 48 "0" (2) 49 "1" (3) 50 "2"
' Note that the LSB is the one with the highest index.
'Version History
' 1.0 6/14/2004 Tested by Russ Tanner
' 1.1 6/26/2004 Tested by Russ Tanner
' Rewrote most of the code to allow much more liberal input values,
' particularly involving decimal points and plus/minus signs.
' Added "Default" parameter to allow psudo-error handling.
' 1.2 6/30/2004 Tested by Russ Tanner
' Added ability to handle virtually unlimited number of leading zeros.
' Also added ability to handle virtually unlimited number of characters after the decimal.
'---
'Initiate variables
Dim C As Long 'Loop counter
Dim eCount As Long 'The number of elements in the parsed byte array
Dim X As String 'String buffer for returned long (before being converted to a long)
Dim RetDefault As Long 'The default return value to use if an error occurs
Dim Y As String 'String buffer for X when removing leading zeros
Dim FoundNZ As Boolean 'Flag used to determine if a non-zero character has been found yet
Dim cChar As String 'The current character we're looking at while removing leading zeros
'Default parameter: Default
'Parameter is missing, set the default return value to zero
If IsMissing(Default) Then
RetDefault = 0
'Parameter is not missing, attempt to convert it to a long
Else
'The specified Default value is a valid type, use it as the default return value
If TypeName(Default) = "Byte" Or TypeName(Default) = "Integer" Or TypeName(Default) = "Long" Then
RetDefault = CLng(Default)
'The specified Default value is a valid type, set the default return value to zero
Else
RetDefault = 0
End If
End If
'Default return value
asLong_ByteArray_ASCII = RetDefault
'Get the element count of the specified byte array
eCount = elementCount(ByteArray)
'Trap: The specified byte array is empty, return zero and exit now
If eCount = 0 Then
asLong_ByteArray_ASCII = 0
Exit Function
End If
'Main loop: Build a string from the ASCII elements of the byte array
For C = 0 To eCount - 1
'Error Trap: The current character is invalid, exit now
If InStr("0123456789.-+", Chr(ByteArray(C))) = 0 Then Exit Function
'Error Trap: The specified value contains more than one decimal point, exit now
If Chr(ByteArray(C)) = "." And InStr(X, ".") > 0 Then Exit Function
'Error Trap: The specified value contains a minus sign (-) within (not before) it, exit now
If Chr(ByteArray(C)) = "-" And C > 0 Then Exit Function
'Error Trap: The specified value contains a plus sign (+) within (not before) it, exit now
If Chr(ByteArray(C)) = "+" And C > 0 Then Exit Function
'The current character is valid, append it to the string accumulator
X = X & Chr(ByteArray(C))
Next C
'The specified value has a decimal, trim it off (no rounding takes place)
If InStr(X, ".") > 0 Then X = Left(X, InStr(X, ".") - 1)
'At this point, we know we have a series of (only) digits.
'The series may or may not have a "+" or "-" prefix.
'Remove all leading zeros from the string (excluding leading minus signs and plus signs)...
'Get the sign of the sequence, if one exists
eCount = Len(X) 'Get the length of the string now
For C = 0 To eCount - 1
'Get the current character we're looking at
cChar = Mid(X, C + 1, 1)
'The FoundNZ flag is set, always append the current character to the output buffer
If FoundNZ Then
Y = Y & cChar
'The FoundNZ flag is not set
Else
'Current character is a non-zero digit or a sign (+/-)
If InStr("123456789+-", cChar) > 0 Then
Y = Y & cChar 'Append it to the output buffer
'Current character is a non-zero digit, set the FoundNZ flag
If InStr("123456789", cChar) > 0 Then FoundNZ = True
End If
End If
Next C
'Restore the data to the original buffer
X = Y
'Trap: The resulting string is either a zero-length string or only a minus sign or only a plus sign,
'these all represent a valid zero value, return zero and exit now
If Len(X) = 0 Or X = "-" Or X = "+" Then
asLong_ByteArray_ASCII = 0
Exit Function
End If
'Trap: The specified value is out of range, exit now
If Len(X) > 12 Then Exit Function 'Too many characters, value must be out of range (-N,NNN,NNN,NNN.)
If CDbl(X) < -2147483648# Or CDbl(X) > 2147483647 Then Exit Function 'Value is out of range
'Convert the specified number to a Long and return the result
asLong_ByteArray_ASCII = CLng(X)
End Function
|
|
|
|
|