From 5fa214950df36ffa77a48ab9367b947e1830b80b Mon Sep 17 00:00:00 2001 From: Raoul Jacobs Date: Thu, 24 Mar 2022 16:53:31 +0100 Subject: [PATCH] add ByteArray To String Functions --- M_omByteArrayFunctions.def | 39 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 M_omByteArrayFunctions.def diff --git a/M_omByteArrayFunctions.def b/M_omByteArrayFunctions.def new file mode 100644 index 0000000..19acccf --- /dev/null +++ b/M_omByteArrayFunctions.def @@ -0,0 +1,39 @@ +Option Compare Database +Option Explicit + +Public Function ByteArrayToHexString(ba() As Byte) As String +Dim i As Long +Dim s As String + + For i = 0 To UBound(ba) + s = s & Right("00" & Hex$(ba(i)), 2) + Next + ByteArrayToHexString = "0x" & s +End Function +Public Function ByteArrayToString(ba() As Byte) As LongLong +Dim i As Long +Dim s As Double + + For i = 0 To UBound(ba) + s = s * 256 + ba(i) + Next + ByteArrayToString = s +End Function + +Public Sub ByteArrayToHexString_Test() + Debug.Print ByteArrayToHexString(RandomByteArray(4)) +End Sub +Public Sub ByteArrayToString_Test() + Debug.Print ByteArrayToString(RandomByteArray(8)) +End Sub + +Private Function RandomByteArray(ByVal arrayLength As Integer) As Byte() +' Demo/helper function to create random byte array + Dim retVal() As Byte, i As Integer + Randomize + ReDim retVal(arrayLength - 1) + For i = 0 To arrayLength - 1 + retVal(i) = CByte(Rnd() * 255) + Next i + RandomByteArray = retVal +End Function \ No newline at end of file