You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求将C#版CRC-16/ARC字符串计算代码转换为VB.NET

Converting CRC-16/ARC C# Code to VB.NET

No worries, I’ve converted plenty of C# CRC implementations to VB.NET before—let’s get this sorted for you. Here’s the direct, equivalent VB.NET code that preserves all the original CRC-16/ARC logic:

Public Function CRCCalculator(dtc As String) As String
    Dim crc As UShort = &H0000
    Dim data As Byte() = GetBytesFromHexString(dtc)
    
    For pos As Integer = 0 To data.Length - 1
        crc = CUShort(crc Xor data(pos))
        
        For i As Integer = 8 To 1 Step -1
            If (crc And &H0001) <> 0 Then
                crc = CUShort(crc >> 1)
                crc = CUShort(crc Xor &HA001)
            Else
                crc = CUShort(crc >> 1)
            End If
        Next
    Next
    
    Return crc.ToString("X4")
End Function

Key Conversion Notes:

  • Variable Declarations: C# ushort maps directly to VB.NET UShort, and we use CUShort() for explicit type casting to avoid implicit conversion warnings (VB is stricter with numeric type handling here).
  • For Loops: VB.NET uses For [var] As [Type] = [start] To [end] Step [increment] syntax—notice the inner loop counts down from 8 to 1, which matches the exact logic of the C# for (var i = 8; i != 0; i--) loop.
  • Bitwise Operations: The XOR (^) and bitwise AND (And) operators work the same way, but we cast back to UShort after shifts and XORs to maintain the correct 16-bit unsigned integer type.
  • String Formatting: ToString("X4") behaves identically in VB.NET to produce a 4-character uppercase hex string, just like the original C# code.

Just a quick heads-up: you’ll need to have your VB.NET version of the GetBytesFromHexString method ready, since the original C# code relies on it to convert the input hex string to a byte array.

内容的提问来源于stack exchange,提问作者Dylan Strohschein

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:11:12