How to get ASCII Dec value from a single character?

BrettNelson

Member
Joined
Apr 8, 2016
Messages
11
Programming Experience
Beginner
Visual Studio 2012
VB.NET
Framework 3.5
I have also unchecked the Microsoft.VisualBasic from the Imported Namespaces


I have two examples below.

Does anyone know if the line that uses the ASCII.GetBytes is an acceptable / reliable way to get an ASCII decimal value from a single alphabet character? Is there an easier, shorter or cleaner way to get the ASCII value of a single alphabet character?

VB.NET:
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

        Dim testChar As String = "a"


        Debug.WriteLine(System.Text.Encoding.ASCII.GetBytes(testChar)(0)) ' this seems to work perfectly, referencing the sole element, 0  


        Debug.WriteLine(Convert.ToInt32("a"c))  ' this one seems cleaner but I can't use the (testChar) variable with it 


    End Sub
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,604
Location
Norway
Programming Experience
10+
this one seems cleaner but I can't use the (testChar) variable with it
That is because testChar is not a Char, it is a String as you declared it.

This is a Char variable:
Dim testChar = "a"c 

This is also a Char variable, assigned the first char in the string "a"
Dim testChar = "a"(0)
 

BrettNelson

Member
Joined
Apr 8, 2016
Messages
11
Programming Experience
Beginner
Thank you so much for the Char variable information. It has helped me quite a bit.

I think I asked the original question wrong. Please let me ask again.

Does anyone know what would be the fastest way, in VB.NET, to write the one line in the loop below? ie: Get the Ascii decimal values from a String variable that has a Length = 1
without using the Microsoft.VisualBasic Namespace?

VB.NET:
[B][FONT=courier new]   For i As Integer = 0 To 30000
      ' can the line below be written to run faster?
      Debug.WriteLine(System.Text.Encoding.ASCII.GetBytes(i.ToString.Substring(0, 1))(0)) 
   Next [/FONT][/B]
 
Last edited:

BrettNelson

Member
Joined
Apr 8, 2016
Messages
11
Programming Experience
Beginner
I took JohnH's 'Char variable' suggestion, played around with it a bit. It helped me find a faster way to get the Ascii decimal values. I'm still not sure what I'm doing though. I'm assuming there are faster ways to do this?

VB.NET:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click        Dim decVal As Integer
        Dim tick As Integer
        Const count As Integer = 35000000

        tick = Environment.TickCount
        For i As Integer = 0 To count
            decVal = System.Text.Encoding.ASCII.GetBytes(i.ToString.Substring(0, 1))(0) ' correct results but not the fastest
        Next
        Debug.WriteLine("Elapsed time1 = " & (Environment.TickCount - tick).ToString)


        tick = Environment.TickCount
        For i As Integer = 0 To count
            decVal = Convert.ToInt32(CChar(i.ToString.Substring(0, 1))) ' this is faster than the above but.. can it be made to run even faster?
        Next
        Debug.WriteLine("Elapsed time2 = " & (Environment.TickCount - tick).ToString)

    End Sub
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,604
Location
Norway
Programming Experience
10+
Convert class belongs to System namespace. You can test if this is faster yourself:
Debug.WriteLine(Convert.ToInt32(i.ToString()(0)))

Of course, if you already have a string to loop through then it will be a lot faster without the ToString operation.

When testing how fast a code runs you should avoid writing to debugger console, that is a really slow operation and something that can vary a lot in time.
 
Top Bottom