How to convert all ASCII written to text

Asked

Viewed 320 times

2

I was able to convert all the text that the user wrote on TextBox to ASCII, but not the other way around. How can I make this conversion from ASCII to text?

Private Sub BtnConvert_Click(sender As Object, e As EventArgs) Handles BtnConvert.Click
    ConvertedTXT.Clear()
    If TxtAscii.Checked = True Then
        While TheLength <> WaitingTXT.Text.Length
            ConvertedTXT.Text += (AscW(WaitingTXT.Text(TheLength)) & " ")
            TheLength += 1
        End While
        TheLength = 0
    ElseIf AsciiTxt.Checked = True Then
        ConvertedTXT.Text += (ChrW(WaitingTXT.Text(TheLength)) & " ")
    End If
End Sub

The code shows the error below:

Code Description Project Filing cabinet
BC32006 "Char" values cannot be converted to "Integer". Use "Microsoft.VisualBasic.Ascw" to interpret a character as a Unicode value or "Microsoft.VisualBasic.Val" to interpret it as a digit. Ascii Converter Form1.Vb

Note: I declared the variable TheLength as Double initialized in 0.

1 answer

0

The example available on Microsoft Developer Network I think it solves your problem:

Dim unicodeString As String = "This string contains the unicode character Pi (" & ChrW(&H3A0) & ")"

' Create two different encodings.
Dim ascii As Encoding = Encoding.ASCII
Dim unicode As Encoding = Encoding.Unicode

' Convert the string into a byte array.
Dim unicodeBytes As Byte() = unicode.GetBytes(unicodeString)

' Perform the conversion from one encoding to the other.
Dim asciiBytes As Byte() = Encoding.Convert(unicode, ascii, unicodeBytes)

' Convert the new byte array into a char array and then into a string.
Dim asciiChars(ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length) - 1) As Char

ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0)

Dim asciiString As New String(asciiChars)

MsgBox(asciiString)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.