3

I want to be able so get the ASCII values of all the characters in a string. I can get the ASCII value of a character but i cannot do it for the whole string.

My attempt:

dim input as string = console.readline()
dim value as integer = ASC(input)

'Then I am changing its value by +1 to write the input in a secret code form.
console.writeline(CHR(value+1))

I want to be able to get the ASCII values for all characters in the string so i can change the all letters in the string +1 character in the alphabet. Any help appreciated, thanks.

4
  • Have you considered that the string is made up from more than one ASCII character code? VB.NET will happily accept your borken code however, ASC only looks at the first character and ignores the rest. Commented Oct 25, 2014 at 22:55
  • Yes but I don't know the command to get the ASCII values for the whole string. I only know how to do so for a single character :/ Commented Oct 25, 2014 at 22:59
  • Well, you could just copy/paste the title of your question into the Google query box and get the same answer you selected. Did you try? Commented Oct 25, 2014 at 23:31
  • A .NET string is a counted sequence of Unicode/UTF-16 code units, one or two of which encode a codepoint. Unicode is a superset of ASCII. When you say "ASCII codes" do you really mean Unicode codepoint? Regardless, in general, adding 1 to a character code can result in an invalid characters code; You need range checking and/or wrap-around logic. Commented Oct 26, 2014 at 17:33

3 Answers 3

2

Use Encoding.ASCII.GetBytes:

Dim asciis As Byte() = System.Text.Encoding.ASCII.GetBytes(input)

If you want to increase each letter's ascii value you can use this code:

For i As Int32 = 0 To asciis.Length - 1
    asciis(i) = CByte(asciis(i) + 1)
Next
Dim result As String = System.Text.Encoding.ASCII.GetString(asciis)
Sign up to request clarification or add additional context in comments.

3 Comments

I am getting an error when compiling the program. " 'Encoding' is not declared. It may be inaccessible due to its protection level."
@Phalanx: if you click on the error Visual Studio would have told you that it's in the System.Text namespace and suggest to add the import. However, you can also use the fully qualified class, edited my answer.
Oh right, I was using the CMD though so i could not see how to fix the error. However your fix works thanks :)
1
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As 
      System.EventArgs) Handles Button2.Click

        Dim a As Integer
        a = Asc(TextBox1.Text)
        MessageBox.Show(a)

    End Sub 
End Class

Comments

1

I know it's been almost three years now, but let me show you this.

Function to convert String to Ascii (You already have a String in your Code). You can use either (Asc) or (Ascw) ... for more information, please read Microsoft Docs

Dim Input As String = console.readline()
Dim Value As New List(Of Integer)
For Each N As Char In Input
Value.Add(Asc(N) & " ")
Next
Dim OutPutsAscii As String = String.Join(" ", Value)

Function to Convert Ascii to String (What you asked for). You can use either (Chr) or (ChrW) ... for more information, please read Microsoft Docs

Dim Value1 As New List(Of Integer)
Dim MyString As String = String.Empty
For Each Val1 As Integer In Value1
MyString &= ChrW(Val1)
Next

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.