简体   繁体   English

将VB转换为C#

[英]Translating VB to C#

I have a cryptographic class written in VB that I am trying to translate into C#. 我有一个用VB编写的加密类,试图将其转换为C#。 In the VB code, there is a block of code: 在VB代码中,有一个代码块:

    ' Allocate byte array to hold our salt.
    Dim salt() As Byte = New Byte(saltLen - 1) {}

    ' Populate salt with cryptographically strong bytes.
    Dim rng As RNGCryptoServiceProvider = New RNGCryptoServiceProvider()

    rng.GetNonZeroBytes(salt)

    ' Split salt length (always one byte) into four two-bit pieces and
    ' store these pieces in the first four bytes of the salt array.
    salt(0) = ((salt(0) And &HFC) Or (saltLen And &H3))
    salt(1) = ((salt(1) And &HF3) Or (saltLen And &HC))
    salt(2) = ((salt(2) And &HCF) Or (saltLen And &H30))
    salt(3) = ((salt(3) And &H3F) Or (saltLen And &HC0))

I translated it into C# and ended up with the following: 我将其翻译为C#,并得到以下结果:

    // Allocate byte array to hold our salt.
    byte[] salt = new byte[saltLen];

    // Populate salt with cryptographically strong bytes.
    RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();

    rng.GetNonZeroBytes(salt);

    // Split salt length (always one byte) into four two-bit pieces and
    // store these pieces in the first four bytes of the salt array.
    salt[0] = ((salt[0] & 0xfc) | (saltLen & 0x3));
    salt[1] = ((salt[1] & 0xf3) | (saltLen & 0xc));
    salt[2] = ((salt[2] & 0xcf) | (saltLen & 0x30));
    salt[3] = ((salt[3] & 0x3f) | (saltLen & 0xc0));

When I try to compile this I get an error on each of the 4 assigns to salt[] - the last 4 lines in the code block. 当我尝试对此进行编译时,在分配给salt []的4个分配中的每个分配上都出现错误-代码块中的最后4行。 The error is: 错误是:

Error 255 Cannot implicitly convert type 'int' to 'byte'. 错误255无法将类型'int'隐式转换为'byte'。 An explicit conversion exists (are you missing a cast?) 存在显式转换(您是否缺少演员表?)

Please forgive the ignorance - I am a relative C# newbie, I tried the following but still got errors: 请原谅我们的无知-我是C#的新手,我尝试了以下操作,但仍然出现错误:

    salt[0] = ((salt[0] & 0xfc as byte) | (saltLen & 0x3 as byte));
    salt[0] = ((salt[0] & (byte)0xfc) | (saltLen & (byte)0x3));

I am not quite sure what this code is doing which perhaps explains why I am unable to figure out how to fix it. 我不太确定这段代码在做什么,这也许可以解释为什么我无法弄清楚如何解决它。

Any help is appreciated. 任何帮助表示赞赏。

Bitwise operators always return int when the operands are int or smaller. 当操作数为int或更小时,按位运算符始终返回int Cast the results to byte : 将结果转换为byte

salt[0] = (byte)((salt[0] & 0xfc) | (saltLen & 0x3));
salt[1] = (byte)((salt[1] & 0xf3) | (saltLen & 0xc));
salt[2] = (byte)((salt[2] & 0xcf) | (saltLen & 0x30));
salt[3] = (byte)((salt[3] & 0x3f) | (saltLen & 0xc0));

I am not quite sure what this code is doing 我不太确定这段代码在做什么

That's more important that getting a syntax that compiles . 获得要编译的语法更重要。 There are enough idiosyncrasies between VB and C# that knowing what the code does so that you can verify the results is more important than just fixing compiler/syntax errors. VB和C#之间有足够的特质,知道代码做什么,以便比单独修复编译器/语法错误更重要的是,可以验证结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM