string 如何在VB.net中获取字符串的ASCII值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26567855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:18:03  来源:igfitidea点击:

How to get ASCII values of a string in VB.net

vb.netstringascii

提问by Phalanx

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.

我希望能够获取字符串中所有字符的 ASCII 值。我可以得到一个字符的 ASCII 值,但我不能对整个字符串这样做。

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.

我希望能够获取字符串中所有字符的 ASCII 值,以便我可以更改字母表中字符串 +1 字符中的所有字母。任何帮助表示赞赏,谢谢。

回答by Tim Schmelter

Use Encoding.ASCII.GetBytes:

使用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:

如果要增加每个字母的 ascii 值,可以使用以下代码:

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)

回答by Funky Gamer

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