Attribute VB_Name = "mdlHexFunctions"
Option Explicit
Public RevBits(255) As Byte 'Used for rapid bit reverse operations
Public RevBitsInitialized As Boolean

Public Function ReverseBits(inByte As Byte) As Byte
  'Reverses the bits in inByte.  This is done via a lookup table for speed
  If Not RevBitsInitialized Then MakeReverseBits 'Initialize the lookup table only once
  ReverseBits = RevBits(inByte)
End Function
Public Sub MakeReverseBits()
    'Initializes the ReverseBits() array.
    'Only needs to be called once
    Dim i As Integer
    Dim j As Integer
    
    For i = 0 To 255
        RevBits(i) = 0
        For j = 0 To 7
            RevBits(i) = RevBits(i) Or ((i \ (2 ^ j)) And 1) * (2 ^ (7 - j))
        Next j
    Next i
    RevBitsInitialized = True
End Sub

Public Function StringToHexString(stringToConvert As String) As String
    Dim results As String
    Dim t As Integer
    For t = 1 To Len(stringToConvert)
        results = results + Hex(AscW(Mid$(stringToConvert, t, 1)))
    Next t
    StringToHexString = results
End Function
Public Function HexStringtoString(hexString As String) As String
    Dim results As String
    Dim t As Integer
    For t = 1 To Len(Trim(hexString)) - 1 Step 2
        results = results + ChrW$(CLng("&H" + Mid$(hexString, t, 2)))
    Next t
    HexStringtoString = results
End Function
Public Function ByteArrayToHexString(byteArray() As Byte) As String
  Dim retVal As String
  Dim t As Integer
  For t = 0 To UBound(byteArray)
    retVal = retVal & HexM2(byteArray(t))
  Next
  ByteArrayToHexString = retVal
End Function

'Converts a string of characters in hex notation to a byte array
 Public Function HexStringToByteArray(ByVal hexString As String) As Byte()
      Dim retVal() As Byte
      Dim t As Integer
      'Allocate storage space for the resulting byte()
      ReDim retVal((Len(hexString) / 2) - 1)
      For t = 1 To Len(hexString) Step 2
        retVal((t - 1) / 2) = (CByte(("&h" & Mid$(hexString, t, 2))))
      Next
      HexStringToByteArray = retVal
    End Function
Public Function HexM2(ByVal Number As Double) As String
    ' Shortcut for simple solution
    If Number = 0 Then
        HexM2 = "00"
        Exit Function
    End If
    Dim retVal As String
    
    ' Keep track of whether result is negative
    Dim Negative As Boolean
    Negative = Number < 0

    ' Put Number in usable form (and discard fractional amount)
    Number = Abs(Int(Number))

    ' Calculate number of hex digits needed for result
    Dim Digits As Double
    Digits = Int(Log(Number) / Log(16)) + 1 ' That's Log16(Number)

    If Negative Then
        ' Add sign and skip ahead one character
        retVal = "-"
    End If

    ' Convert...
    Dim i As Long
    Dim Digit As Long ' Value of current digit
    For i = Digits - 1 To 0 Step -1
        Digit = Int(Number / 16 ^ i)
        Number = Number - Digit * 16 ^ i
        Digit = 48 + Digit - 7 * (Digit > 9) ' Calc ASCII value of character
        retVal = retVal & ChrW(Digit)
    Next
    If Len(retVal) Mod 2 = 1 Then
      retVal = "0" & retVal
    End If
    HexM2 = retVal
End Function



