Below you'll find the source for the Visual Basic 6 function change_endian.
Attribute VB_Name = "modChangeEndian"
' These functions are downloaded from:
' http://www.stefanthoolen.nl/archive/vb6-functions/
'
' You may freely distribute this file but please leave all comments, including this one, in it.
'
' @Author Stefan Thoolen <mail@stefanthoolen.nl>
Option Explicit
''
' Completes a string by adding zero characters to the front
' @param string value The input string
' @param integer length The length the return value must be
' @param string character Optional, the character that should be used for filling, default: "0"
' @author Stefan Thoolen <mail@stefanthoolen.nl>
Public Function zerofill(ByVal value As String, ByVal length As Integer, Optional ByVal character = "0") As String
If Len(value) >= length Then zerofill = value: Exit Function
Dim i As Integer
Do
value = character & value
Loop While Len(value) < length
zerofill = value
End Function
''
' Makes a binary string from an integer number
' Same syntax as the PHP function 'decbin'
' See also: http://www.php.net/manual/en/function.decbin.php
' @param Integer number The decimal value
' @return String A binary presentation of the number (ex.: 00100111)
' @author Stefan Thoolen <mail@stefanthoolen.nl>
Public Function decbin(ByVal number As Integer) As String
Dim retval As String
Do Until number = 0
If (number Mod 2) Then retval = "1" & retval Else retval = "0" & retval
number = number \ 2
Loop
decbin = retval
End Function
''
' Makes an integer number from a binary string
' Same syntax as the PHP function 'bindec'
' See also: http://www.php.net/manual/en/function.bindec.php
' @param String binary_string The binary string (ex.: 00100111)
' @return Integer A decimal presentation of the binary value
' @author Stefan Thoolen <mail@stefanthoolen.nl>
Public Function bindec(binary_string As String) As Long
Dim i As Integer, pos As Integer, ret As Long
For i = 1 To Len(binary_string)
pos = Len(binary_string) - i
If Mid(binary_string, pos + 1, 1) = "1" Then ret = ret + (2 ^ (i - 1))
Next i
bindec = ret
End Function
''
' When reading a binary file in another endian, this function can convert multiple bytes to a valid number
' @param string inp The bytes as string
' @return variant The actual number
' @author Stefan Thoolen <mail@stefanthoolen.nl>
Public Function change_endian(ByVal inp As String, Optional ByVal reverse As Boolean = False) As Variant
If Len(inp) = 0 Then change_endian = 0: Exit Function
Dim i As Integer, s As String
If reverse Then
For i = 1 To Len(inp)
s = s & zerofill(decbin(Asc(Mid(inp, i, 1))), 8)
Next i
Else
For i = Len(inp) To 1 Step -1
s = s & zerofill(decbin(Asc(Mid(inp, i, 1))), 8)
Next i
End If
change_endian = bindec(s)
End Function