CInt问题 - “从数字中投射时,数字必须小于无穷大”

时间:2014-03-04 10:42:00

标签: vb.net casting

将字符串变量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

3 个答案:

答案 0 :(得分:2)

我可能在这里遗漏了一些东西,但我认为你这样做太复杂了。您可以使用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)

答案 1 :(得分:0)

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

    Dim bit As Integer = 111111111

    placevalue1 = Mid(bit, 8, 1)
    pv1 = CInt(placevalue1)

答案 2 :(得分:-1)

字符串只是一个char数组,我们将使用contravarience将char类型转换为byte。

Function CharToByte(ByVal c As Char) As Byte
    Return Convert.ToByte(c)
End Function

Sub Run()
    Dim s As String = "00110001"


    Dim buffer As Byte()
    buffer = s.ToList.ConvertAll(Of Byte)(AddressOf CharToByte).ToArray

    Response.Write("Byte (ASCII): " & String.Join(' ', buffer))
    Response.Write("<br />")

    Dim BitField(8) As Integer
    Dim totalValue As Integer = 0

    BitField = {CType(s.Substring(0, 1), Integer) * 2 ^ 7,
                CType(s.Substring(1, 1), Integer) * 2 ^ 6,
                CType(s.Substring(2, 1), Integer) * 2 ^ 5,
                CType(s.Substring(3, 1), Integer) * 2 ^ 4,
                CType(s.Substring(4, 1), Integer) * 2 ^ 3,
                CType(s.Substring(5, 1), Integer) * 2 ^ 2,
                CType(s.Substring(6, 1), Integer) * 2 ^ 1,
                CType(s.Substring(7, 1), Integer) * 2 ^ 0}


    For Each value As Integer In BitField
        totalValue += value
    Next


    Response.Write("The binary number you entered, " & s & " is " & totalValue & " in decimal")
End Sub