'Declaring a static array of bytes in VBA
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?
Solution 1:[1]
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
Finally you could just do this:
Dim vValues As Variant
vValues = Array(CByte(&H9F), CByte(&H2C), CByte(&H3B), CByte(&HFF), CByte(&H84))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
