"Visual Basic 2015" 将 Ascii 转换为字符失败,为什么
"Visual Basic 2015" convert Ascii to Characters fails why
我在 Visual Basic 中进行以下从数字 Ascii 值到它们各自字符的转换,但失败得很惨:
此处 pth 中给出的值已经转换为 Ascii 值,因此您在变量 textval 变量中看到的是路径的 ascii 等效值
Dim i As Integer
Dim dec As String
Dim original As Integer
Dim sentence As String
Dim inter As String
Dim pth =" c:\TESTER:\JESTER\MESTER "
textval="32 99 58 92 84 69 83 84 69 82 58 92 74 69 83 84 69 82 92 77 69 83 84 69 82 32"
textval = Trim(textval)
'Dim ts() As String = Split(textval)
Dim words As String() = textval.Split(New Char() {" "c})
Label1.Text = words(0) + words(1) + words(2)
original = Int(ts(2))
上面的代码没有给出任何值 words(2) 第二个字符是冒号“:”或 ascii 数字 2961
我怎样才能更正这个问题并使它更通用以接受任何特殊字符?
提前感谢您宝贵的回答。
您没有将整数值转换为字符。你必须使用 Chr
or ChrW
来解决这个问题。请参阅以下代码示例:
解决方案使用Chr
:
Dim textValues1 As String = "32 99 58 92 84 69 83 84 69 82 58 92 74 69 83 84 69 82 92 77 69 83 84 69 82 32"
textValues1 = textValues1.Trim
Dim textWords1 As String() = textValues1.Split(New Char() {" "c})
Dim strValue1 As String = ""
For i As Integer = 0 To UBound(textWords1)
Dim numValue As Integer = 0
If Integer.TryParse(textWords1(i), numValue) Then
strValue1 &= Chr(CInt(numValue))
End If
Next
Debug.Print(strValue1) 'output: c:\TESTER:\JESTER\MESTER
解决方案使用ChrW
:
Dim textValues2 As String = "32 99 58 92 84 69 83 84 69 82 58 92 74 69 83 84 69 82 92 77 69 83 84 69 82 32"
textValues2 = textValues2.Trim
Dim textWords2() As String = textValues2.Split(New Char() {" "c})
Dim strValue2 As String = ""
For j As Integer = 0 To UBound(textWords2)
Dim numValue As Integer = 0
If Integer.TryParse(textWords2(j), numValue) Then
strValue2 &= ChrW(CInt(numValue))
End If
Next
Debug.Print(strValue2) 'output: c:\TESTER:\JESTER\MESTER
这段代码应该会给您一些想法。请注意,整数表示的字符串值在转换之前被验证为整数。
Dim textval As String = "32 a 99 58 92 84 69 83 84 69 82 58 92 74 69 83 84 69 82 92 77 69 83 84 69 82 32"
Dim cvals As New List(Of Char)
For Each s As String In textval.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
Dim ival As Integer
If Integer.TryParse(s, ival) Then
cvals.Add(ChrW(ival))
End If
Next
Dim cv As String = cvals.ToArray ' c:\TESTER:\JESTER\MESTER
此方法验证输入是否有效。
如果您正在寻找通用的 "Visual studio 2015" 答案,请使用 .Net Framework 的现有功能。考虑以下将字符串转换为字节或字符的语句:
Dim bytes = System.Text.ASCIIEncoding.ASCII.GetBytes(pth)
Dim chars = System.Text.ASCIIEncoding.ASCII.GetChars(bytes)
Dim stringValue = System.Text.ASCIIEncoding.ASCII.GetString(bytes)
如果涉及特殊字符,您也可以使用其他编码,例如 UTF8 和 Unicode。这是文档的 link:https://msdn.microsoft.com/en-us/library/system.text.asciiencoding(v=vs.110).aspx
我在 Visual Basic 中进行以下从数字 Ascii 值到它们各自字符的转换,但失败得很惨:
此处 pth 中给出的值已经转换为 Ascii 值,因此您在变量 textval 变量中看到的是路径的 ascii 等效值
Dim i As Integer
Dim dec As String
Dim original As Integer
Dim sentence As String
Dim inter As String
Dim pth =" c:\TESTER:\JESTER\MESTER "
textval="32 99 58 92 84 69 83 84 69 82 58 92 74 69 83 84 69 82 92 77 69 83 84 69 82 32"
textval = Trim(textval)
'Dim ts() As String = Split(textval)
Dim words As String() = textval.Split(New Char() {" "c})
Label1.Text = words(0) + words(1) + words(2)
original = Int(ts(2))
上面的代码没有给出任何值 words(2) 第二个字符是冒号“:”或 ascii 数字 2961
我怎样才能更正这个问题并使它更通用以接受任何特殊字符?
提前感谢您宝贵的回答。
您没有将整数值转换为字符。你必须使用 Chr
or ChrW
来解决这个问题。请参阅以下代码示例:
解决方案使用Chr
:
Dim textValues1 As String = "32 99 58 92 84 69 83 84 69 82 58 92 74 69 83 84 69 82 92 77 69 83 84 69 82 32"
textValues1 = textValues1.Trim
Dim textWords1 As String() = textValues1.Split(New Char() {" "c})
Dim strValue1 As String = ""
For i As Integer = 0 To UBound(textWords1)
Dim numValue As Integer = 0
If Integer.TryParse(textWords1(i), numValue) Then
strValue1 &= Chr(CInt(numValue))
End If
Next
Debug.Print(strValue1) 'output: c:\TESTER:\JESTER\MESTER
解决方案使用ChrW
:
Dim textValues2 As String = "32 99 58 92 84 69 83 84 69 82 58 92 74 69 83 84 69 82 92 77 69 83 84 69 82 32"
textValues2 = textValues2.Trim
Dim textWords2() As String = textValues2.Split(New Char() {" "c})
Dim strValue2 As String = ""
For j As Integer = 0 To UBound(textWords2)
Dim numValue As Integer = 0
If Integer.TryParse(textWords2(j), numValue) Then
strValue2 &= ChrW(CInt(numValue))
End If
Next
Debug.Print(strValue2) 'output: c:\TESTER:\JESTER\MESTER
这段代码应该会给您一些想法。请注意,整数表示的字符串值在转换之前被验证为整数。
Dim textval As String = "32 a 99 58 92 84 69 83 84 69 82 58 92 74 69 83 84 69 82 92 77 69 83 84 69 82 32"
Dim cvals As New List(Of Char)
For Each s As String In textval.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
Dim ival As Integer
If Integer.TryParse(s, ival) Then
cvals.Add(ChrW(ival))
End If
Next
Dim cv As String = cvals.ToArray ' c:\TESTER:\JESTER\MESTER
此方法验证输入是否有效。
如果您正在寻找通用的 "Visual studio 2015" 答案,请使用 .Net Framework 的现有功能。考虑以下将字符串转换为字节或字符的语句:
Dim bytes = System.Text.ASCIIEncoding.ASCII.GetBytes(pth)
Dim chars = System.Text.ASCIIEncoding.ASCII.GetChars(bytes)
Dim stringValue = System.Text.ASCIIEncoding.ASCII.GetString(bytes)
如果涉及特殊字符,您也可以使用其他编码,例如 UTF8 和 Unicode。这是文档的 link:https://msdn.microsoft.com/en-us/library/system.text.asciiencoding(v=vs.110).aspx