I'm able to convert a character to its corresponding character/ASCII code using "Asc(CHAR)". What can I use to convert this returned integer back to its original character form?
Asked
Active
Viewed 2.1e+01k times
37

Peter Mortensen
- 30,738
- 21
- 105
- 131

Freesnöw
- 30,619
- 30
- 89
- 138
-
1Note: `Asc` is not for ASCII precisely but it is sufficient for ASCII. It is for the user's current default code page/character encoding, which varies from user to user, machine to machine and time to time. **In most cases, you'd want to use a fixed character encoding**. VB4/5/6/A/Script/.NET… use **UTF-16** for string elements. Use `AscW` and `ChrW` for that. – Tom Blodget Dec 13 '18 at 23:35
5 Answers
74
The Chr
function in VB.NET converts the integer back to the character:
Dim i As Integer = Asc("x") ' Convert to ASCII integer.
Dim x As Char = Chr(i) ' Convert ASCII integer to char.

acdcjunior
- 132,397
- 37
- 331
- 304

Jason Yost
- 4,807
- 7
- 42
- 65
13
Use the Chr or ChrW function, Chr(charNumber)
.

Peter Mortensen
- 30,738
- 21
- 105
- 131

The Scrum Meister
- 29,681
- 8
- 66
- 64
-
Thank you, I'll give it a try. This sounds exactly like what I need :D – Freesnöw Feb 01 '11 at 01:04
5
you can also use
Dim intValue as integer = 65 ' letter A for instance
Dim strValue As String = Char.ConvertFromUtf32(intValue)
this doesn't requirement Microsoft.VisualBasic reference

Georg
- 404
- 5
- 7
0
Module Module1
Sub Main()
Dim n, n1, sum, rm, cnt As Integer
Console.WriteLine("Enter a Number: ")
n = CInt(Console.ReadLine())
n1 = n
sum = 0
cnt = n1.ToString.Length - 1
While n1 <> 0
rm = n1 Mod 10
sum = sum + rm * CInt(Math.Pow(10, cnt))
cnt = cnt - 1
n1 = n1 10
End While
If n = sum Then
Console.WriteLine("Number is Palindrom")
Else
Console.WriteLine("Number is Palindrom")
End If
End Sub
End Module
-
Hi, this question was correctly answered in 2011, about 12 years ago. While it is usually appreciated to get more answers to the same question, in this case I don't think it is necessary as the precedent solution was exhaustive and precise. – B3S Jun 16 '23 at 12:09