简体   繁体   English

将vb6转换为c#

[英]Conversion vb6 to c#

I am trying to convert some vb6 code to c# and I am struggling a bit. 我试图将一些vb6代码转换为c#,我有点挣扎。 I have looked at this page below and others similar, but am still stumped. 我看过下面的这个页面和其他类似的,但我仍然难过。

Why use hex? 为何使用hex?

vb6 code below: vb6代码如下:

    Dim Cal As String
    Cal = vbNull

    For i = 1 To 8
        Cal = Cal + Hex(Xor1 Xor Xor2)
    Next i

This is my c# code - it still has some errors. 这是我的c#代码 - 它仍然有一些错误。

        string Cal = null;
        int Xor1 = 0;
        int Xor2 = 0;

        for (i = 1; i <= 8; i++)
            {
            Cal = Cal + Convert.Hex(Xor1 ^ Xor2);
            }

The errors are: 错误是:

        Cal = Cal + Convert.Hex(Xor1 ^ Xor2 ^ 6);

Any advice as to why I cant get the hex to convert would be appreciated. 关于为什么我不能让十六进制转换的任何建议将不胜感激。 I suspect its my lack of understanding the .Hex on line 3 above and the "&H" on line 1/2 above. 我怀疑我对上面第3行的.Hex和上面第1/2行的“&H”缺乏了解。

Note : This answer was written at a point where the lines Xor1 = CDec("&H" + Mid(SN1, i, 1)) and Xor1 = Convert.ToDecimal("&H" + SN1.Substring(i, 1)); 注意 :这个答案是在Xor1 = CDec("&H" + Mid(SN1, i, 1))Xor1 = Convert.ToDecimal("&H" + SN1.Substring(i, 1)); were still present in the question. 仍然存在于问题中。


What's the &H ? 什么是&H

In Visual Basic (old VB6 and also VB.NET), hexadecimal constants can be used by prefixing them with &H . 在Visual Basic(旧的VB6和VB.NET)中,十六进制常量可以通过为它们加上&H前缀来使用。 Eg, myValue = &H20 would assign the value 32 to the variable myValue . 例如, myValue = &H20会将值32赋给变量myValue Due to this convention, the conversion functions of VB6 also accepted this notation. 由于这种惯例,VB6的转换函数也接受了这种表示法。 For example, CInt("20") returned the integer 20 , and CInt("&H20") returned the integer 32 . 例如, CInt("20")返回整数20 ,而CInt("&H20")返回整数32

Your code example uses CDec to convert the value to the data type Decimal (actually, to the Decimal subtype of Variant ) and then assigns the result to an integer, causing an implicit conversion. 您的代码示例使用CDec将值转换为数据类型Decimal (实际上,转换为Variant的Decimal子类型),然后将结果分配给整数,从而导致隐式转换。 This is actually not necessary, using CInt would be correct. 这实际上没有必要,使用CInt是正确的。 Apparently, the VB6 code was written by someone who did not understand that (a) the Decimal data type and (b) representing a number in decimal notation are two completely different things. 显然,VB6代码是由不明白(a) Decimal数据类型和(b)表示十进制表示法中的数字的人编写的两个完全不同的东西。


So, how do I convert between strings in hexadecimal notation and number data types in C#? 那么,如何在十六进制表示法和C#中的数字数据类型之间转换字符串?

To convert a hexadecimal string into a number use 要将十六进制字符串转换为数字使用

int number = Convert.ToInt32(hex, 16);   // use this instead of Convert.ToDecimal

In C#, there's no need to pad the value with "&H" in the beginning. 在C#中,无需在开头填充“&H”值。 The second parameter, 16 , tells the conversion function that the value is in base 16 (ie, hexadecimal). 第二个参数16告诉转换函数该值是基数16(即十六进制)。

On the other hand, to convert a number into its hex representation, use 另一方面,要将数字转换为十六进制表示,请使用

string hex = number.ToString("X");       // use this instead of Convert.ToHex

What you are using, Convert.ToDecimal , does something completely different: It converts a value into the decimal data type, which is a special data type used for floating-point numbers with decimal precision. 你正在使用的是Convert.ToDecimal ,它做了一些完全不同的事情:它将一个值转换为decimal数据类型,这是一种特殊的数据类型,用于具有小数精度的浮点数。 That's not what you need. 那不是你需要的。 Your other method, Convert.Hex simply does not exist. 你的另一种方法Convert.Hex根本不存在。

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

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