【问题标题】:Convert Int32 to String将 Int32 转换为字符串
【发布时间】:2018-01-29 19:36:11
【问题描述】:

我有一小段代码可以将字符串转换为 Int32

Dim sample As String = "H۩llo!"
For Each c As Char In sample
    Dim enc As String = Convert.ToInt32(c)
    Console.Write(enc + " ")
Next
Console.ReadKey()

这会将文本转换为 Int32 输出:

72 1769 108 108 111 33

但是有没有办法将输出转换回“H۩llo!”?

【问题讨论】:

  • 另一个选项是Dim chars = int32Chars.Select(Function(n) Char.ConvertFromUtf32(n)),然后你可以使用Dim text = String.Join("", chars)

标签: string vb.net int32


【解决方案1】:

您可以将其转换回原始字符串

Dim nums As Int32() = { 72, 1769, 108, 108, 111, 33 }
Dim cs As Char() = nums.Select(Function(x) Microsoft.VisualBasic.ChrW(x)).ToArray()
Dim s = New String(cs)
Console.OutputEncoding = System.Text.Encoding.UTF8
Console.WriteLine(s)

【讨论】:

  • 这可行,但输出现在是“H?llo!”有什么要退的吗?
  • 不确定您的问题是什么。我在输出中看到了原始字符 ۩。
  • 发现问题,需要设置输出编码。更新答案
  • 在控制台中有一个 [?] 符号但在将其复制到互联网时有一个 ۩ 谢谢! +1
猜你喜欢
  • 2019-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-19
  • 2014-11-15
  • 2014-08-24
  • 2012-06-20
  • 2016-01-26
相关资源
最近更新 更多