# Convertion of list of array to list of integers • ### Question

• Hi,

I have a list of strings contains hex data from 0 to 254. Now I want to convert that string list to integer list. I tried with the example code snippets given in google as shown below.

``````Dim result as list(of string)
Dim intList = result.ConvertAll(Function(str) Int32.Parse(str))``````
I am getting an exception error while running the code

# System.FormatException: 'Input string was not in a correct format.' when converting to integer

my result list contains as follows,

Name Value Type
(0) "abc123b5" String
(1) "abc123b5" String
(2) "abc123b5" String
(3) "abc123b5" String
(4) "abc123b5" String

Now I want to convert the same string list to integer list or double list. can anyone help me to overcome this issue?.

B TEJA

Friday, August 10, 2018 8:09 AM

### All replies

• Hi,

You can try it,Hexadecimal string to int.

```Imports System.Text

Module Module1

Sub Main()
Dim result As New List(Of String)
Dim result2 As New List(Of Integer)
For Each i In result
Next

End Sub
Public Function Conv(ByVal str As String) As Integer

str = Reverse(str)
Dim strList As List(Of String) = New List(Of String)()
For i As Integer = 0 To str.Length - 1
Next

Dim strArr = strList.ToArray()         'Dim sum as integer=0
Dim sum As Double = 0.0

For i As Integer = 0 To strArr.Length - 1

If strArr(i) = "a" OrElse strArr(i) = "A" Then
strArr(i) = "10"
End If

If strArr(i) = "b" OrElse strArr(i) = "B" Then
strArr(i) = "11"
End If

If strArr(i) = "c" OrElse strArr(i) = "C" Then
strArr(i) = "12"
End If

If strArr(i) = "d" OrElse strArr(i) = "D" Then
strArr(i) = "13"
End If

If strArr(i) = "e" OrElse strArr(i) = "E" Then
strArr(i) = "14"
End If

If strArr(i) = "f" OrElse strArr(i) = "F" Then
strArr(i) = "15"
End If

sum = sum + Convert.ToInt32(strArr(i)) * Math.Pow(16, i)
Next
End Function
Public Function Reverse(ByVal str2 As String) As String
Dim sb As StringBuilder = New StringBuilder()
If String.IsNullOrEmpty(str2) Then
Throw New ArgumentException("string is null")
Else
For i = 0 To str2.Length - 1
sb.Append(str2(str2.Length - 1 - i))
Next
End If
Return sb.ToString()
End Function
End Module```

Best Regards,

Alex

MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

• Edited by Friday, August 10, 2018 9:07 AM
Friday, August 10, 2018 8:46 AM
• Hi Alex,

Thanks for your answer. we have a bulk of data(32k samples), for every byte if we rotate these many loops we will lose the data in continuous runs. so is there any optimized code to covert the entire string list with a single call.

Regards,

B TEJA

Friday, August 10, 2018 9:15 AM
• Hi,

I don't think it will lose the data,of course, this requires you to test.

Best Regards,

Alex

MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

Friday, August 10, 2018 9:35 AM
• Hi,

Just now I tested the code by implementing it in a working code. I am getting an error as shown below,

System.InvalidCastException: 'Conversion from string "244ef550" to type 'Integer' is not valid.' at this for <g class="gr_ gr_147 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" data-gr-id="147" id="147">loop</g>.

<g class="gr_ gr_158 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar replaceWithoutSep" data-gr-id="158" id="158">For</g> Each <g class="gr_ gr_159 gr-alert gr_tiny gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" data-gr-id="159" id="159">i</g> In result
Next

Regards,

B TEJA

Friday, August 10, 2018 9:45 AM
• Hi,

sorry，miss a line of code:Return sum

modify the code:

```Imports System.Text

Module Module1

Sub Main()
Dim result As New List(Of String)
Dim result2 As New List(Of Integer)
...
For Each i In result
Next

End Sub
Public Function Conv(ByVal str As String) As Double

str = Reverse(str)
Dim strList As List(Of String) = New List(Of String)()
For i As Integer = 0 To str.Length - 1
Next

Dim strArr = strList.ToArray()
'Dim sum as integer=0
Dim sum As Double = 0.0

For i As Integer = 0 To strArr.Length - 1

If strArr(i) = "a" OrElse strArr(i) = "A" Then
strArr(i) = "10"
End If

If strArr(i) = "b" OrElse strArr(i) = "B" Then
strArr(i) = "11"
End If

If strArr(i) = "c" OrElse strArr(i) = "C" Then
strArr(i) = "12"
End If

If strArr(i) = "d" OrElse strArr(i) = "D" Then
strArr(i) = "13"
End If

If strArr(i) = "e" OrElse strArr(i) = "E" Then
strArr(i) = "14"
End If

If strArr(i) = "f" OrElse strArr(i) = "F" Then
strArr(i) = "15"
End If

sum = sum + Convert.ToInt32(strArr(i)) * Math.Pow(16, i)
Next
Return sum
End Function
Public Function Reverse(ByVal str2 As String) As String
Dim sb As StringBuilder = New StringBuilder()
If String.IsNullOrEmpty(str2) Then
Throw New ArgumentException("string is null")
Else
For i = 0 To str2.Length - 1
sb.Append(str2(str2.Length - 1 - i))
Next
End If
Return sb.ToString()
End Function
End Module```

Best Regards,

Alex

MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

Friday, August 10, 2018 9:58 AM
• Hi,

same error.

Regards,

B TEJA

Friday, August 10, 2018 10:08 AM
• Check an example:

Dim example = "244ef550"

Dim number = Convert.ToInt32(example, 16)

Friday, August 10, 2018 10:35 AM
• You just need to use the overload of Parse that takes a NumberStyles parameter and add AllowHexSpecifier:

```Public Function ConvertHexListToInt(hexList As IEnumerable(Of String)) As IEnumerable(Of Integer)
Dim result = hexList.Select(Of Integer)(Function(str) Int32.Parse(str, Globalization.NumberStyles.AllowHexSpecifier))
Return result.ToArray
End Function
```

```Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim hexList = {"abc123b5", "abc123b5", "abc123b5", "abc123b5", "abc123b5"}
Dim intList = ConvertHexListToInt(hexList)
For Each i In intList
RichTextBox1.AppendText(\$"{i}{vbCrLf}")
Next
End Sub
```

Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

Friday, August 10, 2018 12:06 PM
• Check an example:

Dim example = "244ef550"

Dim number = Convert.ToInt32(example, 16)

This would also work, in addition to adding the AllowHexSpecifier parameter to Integer.Parse()

Either solution should be fine.

Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

Friday, August 10, 2018 12:08 PM