-1

I am trying to convert a floating point binary into a decimal number and i am struggling on how to split the mantissa up into two.

I have the following lines but not sure if it is correct:

 Dim mantissaarray() As String = Split(mantissa,,T)
 Dim first As Integer = CInt(mantissaarray(0))
 Dim second As Integer = CInt(mantissaarray(1))

the third line is where there is an error

System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'

Could someone tell me what i am doing wrong to get this error as i do not understand?

The mantissa is a string e.g 0110 and T is an integer e.g 2 Output to the array should be a split of the string by the third number e.g 01 and 10.

  • Does this answer your question? [Should a retrieval method return 'null' or throw an exception when it can't produce the return value?](https://stackoverflow.com/questions/175532/should-a-retrieval-method-return-null-or-throw-an-exception-when-it-cant-prod) – HolyRandom Dec 14 '19 at 17:33
  • Can you be more precise on what you mean by "mantissa is a binary string"? Do you have an example? – Joel Coehoorn Dec 14 '19 at 17:47
  • 1
    What is the content of `mantissa`? What is `T`? You're splitting on white space. Is that correct? What is `Option Compare` set to? – Jimi Dec 14 '19 at 17:48
  • the mantissa is a string e.g 0110 – Aadil Ahmad Dec 14 '19 at 18:08
  • T is a number eg 2 – Aadil Ahmad Dec 14 '19 at 18:08
  • i don't know what you mean by option compare – Aadil Ahmad Dec 14 '19 at 18:10
  • You cannot split on white space when there are no white spaces. You can write something like `dim mantissaarray() = mantissa.Select(Function(c) CInt(c.ToString())).ToArray()` – Jimi Dec 14 '19 at 18:43
  • what is .select it creates an error – Aadil Ahmad Dec 14 '19 at 19:52
  • It looks like you want the [String.Substring Method](https://learn.microsoft.com/en-us/dotnet/api/system.string.substring?view=netframework-4.8) rather than splitting the string; the latter is normally taken to mean splitting it at occurrences of a particular character. – Andrew Morton Dec 14 '19 at 20:26
  • It's a [Language Integrated Query (LINQ)](https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/linq/introduction-to-linq). You can add a Reference to `System.Linq` in the Project or add `Imports System.Linq` to a class. – Jimi Dec 14 '19 at 20:29
  • Base on your edit, you cannot expect to have that string split into an array of Integers. You can split the string into an array of strings. You could write something like: `dim mantissaarray() = {mantissa.Substring(0, 2), mantissa.Substring(2)}` – Jimi Dec 14 '19 at 23:12

1 Answers1

1

A String in .net is an array of Char. So, using this fact we can do one of the following.

Private Sub OpCode()
    Dim mantissa = "0110"
    'The Linq way
    Dim intArray = (From c In mantissa
                    Select CInt(c.ToString)).ToArray
    For Each i In intArray
        Debug.Print(i.ToString)
    Next
    'The For Each way
    Dim intArray2(mantissa.Length - 1) As Integer
    Dim index As Integer
    For Each c As Char In mantissa
        intArray2(index) = CInt(c.ToString)
        index += 1
    Next
    For Each i In intArray2
        Debug.Print(i.ToString)
    Next
End Sub

Same result.

Mary
  • 14,926
  • 3
  • 18
  • 27