Vb Convert.ToInt32 语法 [英] Vb Convert.ToInt32 syntax

查看:42
本文介绍了Vb Convert.ToInt32 语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码在VB中做了什么?

What does the following code do in VB?

Convert.ToInt32(rmaValidationCode.ToCharArray().GetValue(0), 16) Mod 2) = 1

注意:私有 rmaValidationCode 作为字符串

note :Private rmaValidationCode As String

推荐答案

这其实有点棘手:

让我们把它分开:

rmaValidationCode.ToCharArray().GetValue(0)

获取字符串中的第一个字符.为简单起见,用 c 替换该表达式:

gets the first character in the string. Replacing that expression with c for simplicity:

Convert.ToInt32(c, 16)

很有趣...因为 Convert.ToInt32(char, int) 没有重载.相反,VB 编译器插入从 CharString 的隐式转换,然后调用 Convert.ToInt32(string, int).在这种情况下,它将其解析为十六进制.所以我们已经将字符串的第一个字符解析为十六进制数字(在将第一个字符转换回字符串之后)".现在让我们用 x 替换该表达式:

is interesting... because there's no overload to Convert.ToInt32(char, int). Instead, the VB compiler inserts an implicit conversion from Char to String and then calls Convert.ToInt32(string, int). In this case it's parsing it as hex. So we've got "parse the first character of the string as a hex digit (after converting that first character back to a string)". Now let's substitute that expression with x:

(x Mod 2) = 1

只取除以 2 后的余数并测试它是否为 1.

that just takes the remainder after dividing by 2 and tests for it being 1.

总的来说,这段代码测试字符串中的第一个字符是 1、3、5、7、9、B、D 还是 F(不区分大小写).如果是这样,结果为真.如果第一个字符是 0、2、4、6、8、A、C 或 E,则结果为假.如果字符串为空、为空或第一个字符不是列出的字符,则会抛出异常.

So overall, this code tests whether the first character in the string is 1, 3, 5, 7, 9, B, D or F (case-insensitively). If so, the result is true. If the first character is 0, 2, 4, 6, 8, A, C, or E, the result is false. If the string is null, empty or the first character is none of the ones listed, an exception will be thrown.

这篇关于Vb Convert.ToInt32 语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆