String.ToCharArray() 存储在 StringCollection 中转换为数字并返回到 VB.net

发布于 2024-08-03 17:49:11 字数 209 浏览 3 评论 0原文

我有一个字符串,已转换为字符数组并存储在字符串集合中,如何返回每个字符串中字符的数值?如何反转这个过程,从数值中获取字符?

我知道我可以编写一个 Select Case 语句,但这需要很长时间,因为我需要涵盖人们可能想要在英语中使用的每个字符,包括标点符号。

vb.net 中是否已经内置了一个方法来执行此操作?

感谢您的帮助!

I have a string which was converted to a char array and stored in a stringcollection, how do I return the numerical value for the character in each string? How can I reverse this process, getting the character from the numerical value?

I know I could code a Select Case statement, but that would take a very long time as I need to cover every character a person could want to conceivably use in the English language, including punctuation.

Is there already a method built into vb.net for doing this?

Thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安静 2024-08-10 17:49:11

Convert 类具有可在字符和整数之间进行转换的方法:

Dim c As Char = "A"C
Dim i As Integer = Convert.ToInt32(c)
Dim c2 As Char = Convert.ToChar(i)

要将字符串中的字符转换为整数数组的值循环:

Dim codes(theString.Length - 1) As Integer
For i As Integer = 0 to theString.Length - 1
    codes(i) = Convert.ToInt32(theString.Chars(i))
Next

The Convert class has methods that can convert between characters and integers:

Dim c As Char = "A"C
Dim i As Integer = Convert.ToInt32(c)
Dim c2 As Char = Convert.ToChar(i)

To loop the values converted from the characters in a string into an array of integers:

Dim codes(theString.Length - 1) As Integer
For i As Integer = 0 to theString.Length - 1
    codes(i) = Convert.ToInt32(theString.Chars(i))
Next
一江春梦 2024-08-10 17:49:11

尝试这样的操作:

For Each c As Char In yourString
    Dim i As Int32 = DirectCast(c, Int32)
Next

请记住,System.String 实现了 IEnumerable,因此对它进行 For Each 是合法的。字符和数字之间的转换就像在 System.CharSystem.Int32 之间进行转换一样简单(这里我展示了如何获取每个字符的数值)字符串)。

Try something like this:

For Each c As Char In yourString
    Dim i As Int32 = DirectCast(c, Int32)
Next

Remeber that System.String implements IEnumerable<Char> so it is legal to For Each over it. And converting between a character and a number is as simple as casting between System.Char and System.Int32 (here I have shown how to get the numeric value for each character in the string).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文