Due to the fact all data to/from the Max! Gateway (Cube) is Base64 coded, a proper functioning HexToBase64 and Base64ToHex function looks very necessary to me. It took me a long time to get a HexToBase64 and Base64ToHex function in vb for Homeseer, but it seems to work properly now:
Code: Select all
Sub Main(parm as object)
'b64 is a string containing base-64 encoded data
Dim b64 As String = "CwA6FwASGUAqAAAACwA13QASGQoqAAAA"
'convert base64 string to hex string
Dim hx As String = Base64ToHex(b64)
'convert hex string to base64 string
Dim b64b As String = HexToBase64(hx)
'Display the converted strings
hs.WriteLog("Info", b64 & " " & hx & " " & b64b)
End Sub
Private Function Base64ToHex(base64string As String) As String
Dim ret As String
Dim b64bytes() As Byte = System.Convert.FromBase64String(base64string)
For Each b As Byte In b64bytes
ret &= Hex(b).PadLeft(2, "0"c)
Next
Return ret
End Function
Private Function HexToBase64(ByVal HexText As String) As String
Dim HexTextBytes As Integer = HexText.Length \ 2 -1
Dim result(HexTextBytes) As Byte
Dim i As Integer
For i = 0 To HexTextBytes
result(i) = Convert.ToByte(HexText.Substring(i * 2, 2), 16)
Next
Return Convert.ToBase64String(result)
End Function