Puneet Mathur Puneet Mathur -4 years ago 39
Vb.net Question

"Visual Basic 2015" convert Ascii to Characters fails why

I am doing the following conversion from Numeric Ascii values to their respective characters in Visual Basic but it fails miserably:

Here the value given in pth has already been converted to Ascii values so what you see in the variable textval variable is the ascii equivalint of the path

Dim i As Integer
Dim dec As String
Dim original As Integer
Dim sentence As String
Dim inter As String
Dim pth =" c:\TESTER:\JESTER\MESTER "
textval="32 99 58 92 84 69 83 84 69 82 58 92 74 69 83 84 69 82 92 77 69 83 84 69 82 32"
textval = Trim(textval)
'Dim ts() As String = Split(textval)
Dim words As String() = textval.Split(New Char() {" "c})
Label1.Text = words(0) + words(1) + words(2)
original = Int(ts(2))


the above code does not give any value for words(2) the second character which is a COLON ":" or for the ascii number 2961

How Can I correct this problem and make this more universal to take any special characters as well?

Thanks in advance for your valuable answers.

Answer Source

You don't convert the integer value to a character. You have to use Chr or ChrW to solve this. See the following code example:

solution using Chr:

Dim textValues1 As String = "32 99 58 92 84 69 83 84 69 82 58 92 74 69 83 84 69 82 92 77 69 83 84 69 82 32"
textValues1 = textValues1.Trim
Dim textWords1 As String() = textValues1.Split(New Char() {" "c})
Dim strValue1 As String = ""

For i As Integer = 0 To UBound(textWords1)
    Dim numValue As Integer = 0

    If Integer.TryParse(textWords1(i), numValue) Then
        strValue1 &= Chr(CInt(numValue))
    End If
Next

Debug.Print(strValue1) 'output:  c:\TESTER:\JESTER\MESTER 

solution using ChrW:

Dim textValues2 As String = "32 99 58 92 84 69 83 84 69 82 58 92 74 69 83 84 69 82 92 77 69 83 84 69 82 32"
textValues2 = textValues2.Trim
Dim textWords2() As String = textValues2.Split(New Char() {" "c})
Dim strValue2 As String = ""

For j As Integer = 0 To UBound(textWords2)
    Dim numValue As Integer = 0

    If Integer.TryParse(textWords2(j), numValue) Then
        strValue2 &= ChrW(CInt(numValue))
    End If
Next

Debug.Print(strValue2) 'output: c:\TESTER:\JESTER\MESTER 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download