VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



NumtoBytesEx: Convertes any unsinged Long or unsigned Double number into array of bytes.

by Imran Zaheer (2 Submissions)
Category: Math/Dates
Compatability: Visual Basic 4.0 (32-bit)
Difficulty: Unknown Difficulty
Originally Published: Wed 3rd October 2001
Date Added: Mon 8th February 2021
Rating: (1 Votes)

NumtoBytesEx: Convertes any unsinged Long or unsigned Double number into array of bytes.

Rate NumtoBytesEx: Convertes any unsinged Long or unsigned Double number into array of bytes.



' Author                : Imran Zaheer
' Contact               : [email protected]
' Date                  : March 2001
' Function BytesToNumEx : Convertes any unsinged Long or unsigned Double
'                         number into array of bytes
'
'
' Parameters :
'  (All parameters are reuuired: No Optional)
'     numValue    : a number (unsinged long or unsinged double)
'     retArray    : a dynamic array of bytes, after execution of
'                   this procedure this array contains the resultant
'                   bytes
'
' Example :
'      dim myArray() as byte
'      dim i as Integer, lngVal as Long
'      lngVal= 168735934
'      Call NumToBytesEx(lngVal, myArray)
'      For i = LBound(myArray) To UBound(myArray)
'          MsgBox myArray(i)
'      Next i

Sub NumToBytesEx(numValue As Double, retArray() As Byte)
Dim i As Integer
Dim lng256 As Double, loopEnd As Integer
Dim ByteArray() As Byte
Dim strTmp As String
Dim lngVal As Long, dblVal As Double

    On Error Resume Next
    lngVal = CLng(numValue)
    If Err.Number <> 0 Then
        On Error GoTo 0
        On Error Resume Next
        dblVal = CDbl(numValue)
        If Err.Number <> 0 Then
            MsgBox "The datatype could not be determined ...!", vbCritical
            Exit Sub
        Else
            loopEnd = 8
        End If
    Else
        loopEnd = 4
    End If
    On Error GoTo 0
    
    lng256 = 256
    lng256 = lng256 ^ (loopEnd - 1)
    
    ReDim ByteArray(1 To loopEnd)
    
    For i = 1 To loopEnd
        ByteArray(i) = Fix(numValue / lng256)
        numValue = numValue - (ByteArray(i) * lng256)
        lng256 = lng256 / 256
    Next i
    
    retArray = ByteArray
End Sub


Download this snippet    Add to My Saved Code

NumtoBytesEx: Convertes any unsinged Long or unsigned Double number into array of bytes. Comments

No comments have been posted about NumtoBytesEx: Convertes any unsinged Long or unsigned Double number into array of bytes.. Why not be the first to post a comment about NumtoBytesEx: Convertes any unsinged Long or unsigned Double number into array of bytes..

Post your comment

Subject:
Message:
0/1000 characters