I can declare a static byte array in VBA like this, but it appears that the elements are not of type byte.
Dim A As Variant
A = Array(&H9F, &H2C, &H3B, &HFF, &H84)
I can solve this by copying the content to a second array, but that doesn't look very efficient to me.
Dim A As Variant
A = Array(&H9F, &H2C, &H3B, &HFF, &H84)
Dim N As Long: N = UBound(A)
Dim B() As Byte
ReDim B(N)
For X = 0 To N
B(X) = A(X)
Next
So my question is: is there a better or more elegant way to do this?
CodePudding user response:
ok, probably not the solution you were looking for, but you can convert a string directly to a byte array..
So, for your example, something (crazy) like this will work..
Dim bResult() As Byte
bResult = StrConv(Join(Array(Chr(&H9F), Chr(&H2C), Chr(&H3B), Chr(&HFF), Chr(&H84)), vbNullString), vbFromUnicode)
A second method would be to update the elements in the original array. it still requires a loop, but the entries will then by Bytes
Dim i As Long
Dim vValues As Variant: vValues = Array(&H9F, &H2C, &H3B, &HFF, &H84)
For i = LBound(vValues) To UBound(vValues)
vValues(i) = CByte(vValues(i))
Next i