使用 复制数组.网民

本文关键字:网民 复制数组 使用 | 更新日期: 2023-09-27 18:31:43

如何使用.netmf(VB或C#)多次重复数组?

下面你会发现我用来构建 Byte() 的函数。它所做的只是获取一个 8 个字符的字符串 (Ex.10101000),并将每个字符替换为 252,将 0 替换为 128,并将其转储到字节数组中。前任。

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

使用 复制数组.网民

想通了,我把它包装在另一个函数中。通过使用 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