与 CInt 的问题 - "When casting from a number, the number must be less than infinity"



将值从字符串变量placevalue1传递到整数变量时,pv1出现错误" When casting from a number, the number must be less than infinity "。

发现和尝试的一切都没有解决我的问题。

Sub converttobinary1()
    Console.WriteLine(bit)
    Dim placevalue1, placevalue2, placevalue3, placevalue4, placevalue5, placevalue6, placevalue7, placevalue8 As String
    Dim pv1, pv2, pv3, pv4, pv5, pv6, pv7, pv8 As Integer
    'Convert.ToInt32(bit)
    placevalue1 = Mid(bit, 8, 1)
    pv1 = (CInt(placevalue1)) * 1 'I've tried this......
    'pv1 = placevalue1 * 1
    placevalue2 = Mid(bit, 7, 1)
    pv2 = CInt(placevalue2)      'and this......
    'pv2 = placevalue1 * 2
    placevalue3 = Mid(bit, 6, 1)
    pv3 = CInt(placevalue3)
    'pv3 = placevalue1 * 4
    placevalue4 = Mid(bit, 5, 1)
    pv4 = CInt(placevalue4)
    'pv4 = placevalue1 * 8
    placevalue5 = Mid(bit, 4, 1)
    pv5 = CInt(placevalue5)
    'pv5 = placevalue1 * 16
    placevalue6 = Mid(bit, 3, 1)
    pv6 = CInt(placevalue6)
    'pv6 = placevalue1 * 32
    placevalue7 = Mid(bit, 2, 1)
    pv7 = CInt(placevalue7)
    'pv7 = placevalue1 * 64
    placevalue8 = Mid(bit, 1, 1)
    pv8 = CInt(placevalue8)
    'pv8 = placevalue1 * 128
    'Console.WriteLine(bit)
    denary = placevalue1 + placevalue2 + placevalue3 + placevalue4 + placevalue5 + placevalue6 + placevalue7 + placevalue8
    Console.WriteLine("The binary number you entered, " & bit & " is " & denary & " in denary (or Base 10)")
    ' Console.WriteLine(denary)
    ' Console.ReadLine()
End Sub

我可能在这里遗漏了一些东西,但我认为你把这种方式弄得太复杂了。可以使用 Convert.ToInt 在 .NET 中的基础之间进行转换:

    Dim s As String = "11111111"
    Dim denary As Integer = Convert.ToInt32(s, 2)
    Debug.Write("The binary number you entered, " & s & " is " & denary & " in denary (or Base 10)")

输出

The binary number you entered, 11111111 is 255 in denary (or Base 10)

可能您的bit变量未初始化。尝试分配值

    Dim bit As Integer = 111111111
    placevalue1 = Mid(bit, 8, 1)
    pv1 = CInt(placevalue1)

相关内容

最新更新