Results 1 to 9 of 9

Thread: [RESOLVED] Problem with Asc()

  1. #1

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,429

    Resolved [RESOLVED] Problem with Asc()

    Hi,

    I have a little tool which I use to read the ASCII value of individual characters in a string. The form includes a TextBox and a Button.
    I enter a word to check into TextBox1, and click Button2 to read the ASCII value of each successive character in turn.
    vb.NET Code:
    1. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    2.         Static a As Integer = 0
    3.         If a = TextBox1.TextLength - 1 Then Label1.Text = "Ok." : Exit Sub
    4.         Dim z As Char = CChar(TextBox1.Text.Substring(a, 1))
    5.         Dim b As Integer = Asc(z)
    6.         Dim y As String = Chr(b)
    7.         Label1.Text = b.ToString
    8.         a += 1
    9.         End Sub
    Entering the string Рахів and running the tool with a breakpoint at Line 9, the results I get are: -

    z = "Р"c Char: b = 63 Integer: y = "?" String
    z = "а"c Char: b = 63 Integer: y = "?" String
    z = "х"c Char: b = 63 Integer: y = "?" String
    z = "і"c Char: b = 63 Integer: y = "?" String
    z = vbNullChar Char: b = 0 Integer: y = Nothing String

    I don't understand these results.


    Poppa
    Last edited by Poppa Mintin; May 20th, 2022 at 06:53 AM. Reason: Posted instead of previewing !
    Along with the sunshine there has to be a little rain sometime.

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: Problem with Asc()

    I'm going to give you the best piece of advice you've ever gotten, please for the love of God and everything sacred in this life stop using ASCII. ASCII is a dinosaur that the world has forgotten about. It is the steam engine of String formats. Modern Strings are Unicode Strings, not ASCII. ASCII code in the modern age only appear to work correctly until they encounter Strings that use characters that cannot map cleanly to ASCII/ANSI codes.

    Right now I'm in the middle of doing an exam so I can't go into details like I usually do with code snippets and deep explanations but I'm sure there are other members here that can help you formulate a better way to do what you're doing. The primary problem with your code just from looking at it is that it doesn't treat Strings like a collection of 2 byte Unicode encoded characters which it should be doing. I'm not surprised you're running into problems.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,474

    Re: Problem with Asc()

    Quote Originally Posted by Poppa Mintin View Post
    Hi,

    I have a little tool which I use to read the ASCII value of individual characters in a string. The form includes a TextBox and a Button.
    I enter a word to check into TextBox1, and click Button2 to read the ASCII value of each successive character in turn.
    vb.NET Code:
    1. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    2.         Static a As Integer = 0
    3.         If a = TextBox1.TextLength - 1 Then Label1.Text = "Ok." : Exit Sub
    4.         Dim z As Char = CChar(TextBox1.Text.Substring(a, 1))
    5.         Dim b As Integer = Asc(z)
    6.         Dim y As String = Chr(b)
    7.         Label1.Text = b.ToString
    8.         a += 1
    9.         End Sub
    Entering the string Рахів and running the tool with a breakpoint at Line 9, the results I get are: -

    z = "Р"c Char: b = 63 Integer: y = "?" String
    z = "а"c Char: b = 63 Integer: y = "?" String
    z = "х"c Char: b = 63 Integer: y = "?" String
    z = "і"c Char: b = 63 Integer: y = "?" String
    z = vbNullChar Char: b = 0 Integer: y = Nothing String

    I don't understand these results.


    Poppa
    I completely agree with Niya's comments regarding Unicode and that really is the correct way to handle strings.

    However, I am unclear on what you are trying to do with these lines of code
    Code:
    Dim z As Char = CChar(TextBox1.Text.Substring(a, 1))
    Dim b As Integer = Asc(z)
    Dim y As String = Chr(b)
    If z is a Char, what do you expect turning it into it's ASCII code and then turning it back into a Char will do? Ideally it will just return the same character as is stored in the variable z, however this will fail if the character isn't an ASCII character, if codepage translations aren't reversible, or might even throw an exception if the Asc() call can't actually map the character.

    It might be more helpful if you explain what you are trying to achieve, what do you want the end result to be?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Problem with Asc()

    At the very least, use AscW and ChrW but don't do that either. Convert.ToInt32 will convert a Char to a Unicode value and Convert.ToChar will do the opposite. That's what you should be using, not those VB6 holdovers.

    Also, this:
    vb.net Code:
    1. Dim z As Char = CChar(TextBox1.Text.Substring(a, 1))
    can be replaced with this:
    vb.net Code:
    1. Dim z = TextBox1.Text(a)

  5. #5

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,429

    Re: Problem with Asc()

    Quote Originally Posted by Niya View Post
    stop using ASCII[/I]. ASCII is a dinosaur that the world has forgotten about. It is the steam engine of String formats. Modern Strings are Unicode Strings, not ASCII. ASCII code in the modern age only appear to work correctly until they encounter Strings that use characters that cannot map cleanly to ASCII/ANSI codes.
    Thanks for the advice Niya... However, that's far far easier said than done. I can't find anything anywhere which explains how to amend my code to accomplish the task. It certainly doesn't help when (Despite having VB selected) the examples are in C# !

    We dinosaurs tend to use dinosaur code.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: Problem with Asc()

    What you were told

    Code:
        Private txt1IDX As Integer = 0
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            If txt1IDX >= TextBox1.TextLength Then
                txt1IDX = 0
                Label1.Text = "Ok."
                Exit Sub
            End If
            Dim b As Integer = Convert.ToUInt32(TextBox1.Text(txt1IDX))
            Label1.Text = b.ToString
            txt1IDX += 1
        End Sub
    
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            txt1IDX = 0
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,474

    Re: Problem with Asc()

    Quote Originally Posted by Poppa Mintin View Post
    Thanks for the advice Niya... However, that's far far easier said than done. I can't find anything anywhere which explains how to amend my code to accomplish the task. It certainly doesn't help when (Despite having VB selected) the examples are in C# !

    We dinosaurs tend to use dinosaur code.


    Poppa
    If the examples are in C# then they might be even easier to convert than trying to use legacy VB6 methods. What C# snippets are you trying to convert?

  8. #8

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,429

    Re: Problem with Asc()

    Quote Originally Posted by PlausiblyDamp View Post
    If the examples are in C# then they might be even easier to convert than trying to use legacy VB6 methods. What C# snippets are you trying to convert?
    Hi PlausiblyDamp,

    I wasn't trying to convert 'em, I was just complaining that examples in a vb.NET text were in C#.

    Pop.
    Along with the sunshine there has to be a little rain sometime.

  9. #9

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,429

    Re: Problem with Asc()

    Thanks all,

    I am often accused of using VB6 code... I have Never used VB6 for anything...
    This dinosaur still thinks in terms of VB3.0.

    Thanks again to John, always useful advice.

    Poppa
    Along with the sunshine there has to be a little rain sometime.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width