如何使用.netmf(VB或C#)多次重复数组?
下面你会找到我用来构建Byte()的函数。它所做的就是得到一个8字符串(Ex.10101000)并将每个字符串分别用252替换1和用128替换0并将其转储为字节数组。实施例
Dim bytes() as array = {252,128,252,128,252,128,128,128}
^^^^相当于。^^^^
所以说我使用我的函数,然后传递一串10101011
Dim N as integer = 3
Dim Teststring as string = "10101011"
Dim testbyte() as byte = Allbytes2send(Teststring , N)
Testbyte()现在应该用相位字符串填充3次
For each b in Testbyte
Debug.Print(b.tostring)
Next
'{252,128,252,128,252,128,252,252,252,128,252,128,252,128,252,252,252,128,252,128,252,128,252,252}
功能
Shared Function Allbytes2send(ByVal color As String) As Byte()
Dim bitcolor As String() = New String(color.Length - 1) {}
Dim b2sa As Byte() = New Byte(bitcolor.Length - 1) {}
For i As Integer = 0 To color.Length - 1
bitcolor(i) = color(i).ToString()
Next
For i As Integer = 0 To bitcolor.Length - 1
Dim data As String = bitcolor(i)
Select Case data
Case "0"
bitcolor(i) = "128"
Case "1"
bitcolor(i) = "252"
End Select
Next
For i As Integer = 0 To bitcolor.Length - 1
b2sa(i) = Byte.Parse(bitcolor(i))
Next
Return b2sa
End Function
答案 0 :(得分:0)
想出来我把它包装在另一个函数中。通过使用Array.copy并重新分配我的字节数组,我能够动态地改变数组的大小,并使用预定数乘以任何给定的数字进行复制。
Shared Function SendAllLeds(ByVal LedsData As Byte(), ByVal LedCount As Integer) As Byte()
Dim testbyte2() As Byte = Nothing
Dim y, z As Integer
y = (24 * LedCount) - 1
z = 0
ReDim testbyte2(y)
While z <= y
Array.Copy(LedsData, 0, testbyte2, z, 24)
z += 24
End While
Return testbyte2
End Function