【问题标题】:Convert bit string array to byte array将位字符串数组转换为字节数组
【发布时间】:2019-06-26 18:59:14
【问题描述】:

我有以下二进制字符串(实际上是位数组) "1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0"

我想将其转换为字节数组。 对于只接受字节数组的嵌入式报告代码函数,我需要它。我已经从 powershell 转换了这个函数。

我找到了这个,但它是 C#

string source = "1,1,1,0,0";

 byte[] result = source
   .Split(',')
   .Select(item => byte.Parse(item))
   .ToArray();

这在报表生成器中不起作用(“Select”不是“System.Array”的成员。)

Dim source As String = "1,1,1,0,0"
Dim result As Byte() = source.Split(","c).[Select](Function(item) Byte.Parse(item)).ToArray()

如果我按照建议添加 System.Linq,我仍然会收到错误消息: “Select”不是“Linq”的成员。

Dim source As String() = "1,1,1,0,0".Split(","c)
Dim result As Byte() = System.Linq.Select(Function(source) Byte.Parse(source)).ToArray()

【问题讨论】:

  • 我投票决定将此问题作为离题结束,因为这只不过是对代码转换的隐晦要求。有很多在线工具可以执行这项服务。
  • @TnTinMn,我没有看到任何面纱。
  • 您可以使用online converter将VB转换为C#或将C#转换为VB。
  • 感谢@TnTinMn,您非常有帮助。如果您费心阅读文本,您会看到我已经从 powershell 转换了整个函数,而没有任何人的帮助,而 vb 知识为零。所以我在这里看不到任何面纱。我只是想要一些帮助,因为报告服务不理解任何不是字符串的东西。
  • @Han 很抱歉我没有指定这个,但我使用了转换器。不幸的是,代码没有正确转换。

标签: vb.net reporting-services


【解决方案1】:

更仔细地查看函数后,我意识到输入可以是任何数组,因此我将数组的类型修改为字符串并相应地更新了我的输入。坦克你们试图提供帮助。

功能

Function GetSQLProductKey(ByVal astrBinaryKey As String(), ByVal intVersion As Integer) As String
    Dim achrKeyChars As Char() = {"B", "C", "D", "F", "G", "H", "J", "K", "M", "P", "Q", "R", "T", "V", "W", "X", "Y", "2", "3", "4", "6", "7", "8", "9"}
    Dim strSQLProductKey As String
    Dim iastrBinaryKey As Long
    Dim iachrKeyChars As Long
    Dim iastrBinaryKeyOuterLoop As Long
    Dim iastrBinaryKeyInnerLoop As Long
    Try
        If (intVersion >= 11) Then
            iastrBinaryKey = 0
        Else
            iastrBinaryKey = 52
        End If
        For iastrBinaryKeyOuterLoop = 24 To 0 Step -1
            iachrKeyChars = 0
            For iastrBinaryKeyInnerLoop = 14 To 0 Step -1
                iachrKeyChars = iachrKeyChars * 256 Xor astrBinaryKey(iastrBinaryKeyInnerLoop + iastrBinaryKey)
                astrBinaryKey(iastrBinaryKeyInnerLoop + iastrBinaryKey) = Math.Truncate(iachrKeyChars / 24)
                iachrKeyChars = iachrKeyChars Mod 24
            Next iastrBinaryKeyInnerLoop
            strSQLProductKey = achrKeyChars(iachrKeyChars) + strSQLProductKey
            If (iastrBinaryKeyOuterLoop Mod 5) = 0 And iastrBinaryKeyOuterLoop <> 0 Then
                strSQLProductKey = "-" + strSQLProductKey
            End If
        Next iastrBinaryKeyOuterLoop
    Catch
        strSQLProductKey = "Cannot decode product key."
    End Try
    GetSQLProductKey = strSQLProductKey
End Function

函数调用 SSRS

Code.GetSQLProductKey(Split(Replace(Fields!ProductKey.Value, " ",""), ","), Left(Fields!Version.Value, 2))

【讨论】:

    猜你喜欢
    • 2023-04-09
    • 1970-01-01
    • 2018-07-16
    • 2021-11-11
    • 2021-07-09
    • 1970-01-01
    • 2018-10-31
    相关资源
    最近更新 更多