填充右边的"& HBB" VB.Net

时间:2017-07-14 18:56:39

标签: vb.net

我正在编写vb.net以正确填充值。我的代码如下:

 Dim Ni As Byte() = { &HA6, &H02, &H01,....}    
 Dim Nca As Byte()  = { &H12, &H00, &H02,....}    
 If (Ni.Length <= Nca.Length - 36) Then

        Dim padLength As Integer  = Nca.Length - 36 - Ni.Length
        Dim padvalue As Byte() = {&HBB}
        For Each w As Integer In padLength
            Console.Write(Ni =Ni+ Ni.rigtPadding({&HBB}))
        Next

 End If

我的代码无效。如果Ni.Length&lt; = Nca.Length-36,我需要用Nca-36-Ni字节的十六进制值&#34;&amp; HBB&#34;来填充右边的全Ni。它可能超过1&#34;&amp; HBB&#34;需要在右侧填充。然后我需要Ni的最终结果和填充。

Ni是发行人公钥,Nca是CA公钥,我想要的最终结果应该是{&amp; H6A,02,11,FF,.....,BB,BB,BB}

非常感谢您的任何帮助和评论。

2 个答案:

答案 0 :(得分:2)

由于您使用常规数组,因此相对简单(但使用List(Of T)可能更简单。)

首先,您需要调用Array.Resize()以增加数组的大小。然后,您可以使用常规For loop填充新空间。

Dim prevLength As Integer = Ni.Length 'Store the previous length so we can start looping from it.
Array.Resize(Ni, Ni.Length + padLength) 'Take the original length and increase it by how much needs to be padded.

'Iterate from the beginning of the padding to the new end of the array.
For i = prevLength To Ni.Length - 1
    Ni(i) = &HBB
Next

我还建议你阅读一些有关数组如何工作的内容:Arrays in Visual Basic - Microsoft Docs

答案 1 :(得分:1)

您可以使用Redim Preserve手动调整NCI大小并设置填充字符:

Dim Padding as Byte = &HBB
Dim Nca As Byte()  = { &H12, &H00, &H02,....}
Dim Ni As Byte() = { &HA6, &H02, &H01,....} 
Dim len = Nca.Length - 36 
if (len > Ni.Length) Then
    Redim Preserve Ni(len+1)
    For i As Integer = len to Ni.Length-1
        Ni(i) = Padding
    Next
End if

或者您可以使用LINQ:

 Dim Padding as Byte = &HBB
 Dim Pad =Function(i) If(i < Nca.Length, Nca(i), padding)
 Dim Nca As Byte()  = { &H12, &H00, &H02,....} 
 Dim len as integer = Nca.Length - 36  
 if len > Ni.Length Then Ni = Enumerable.Range(0, len+1).Select(Pad).ToArray()