Results 1 to 5 of 5

Thread: Comparing strings

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2021
    Posts
    5

    Comparing strings

    I'm modifying an old VB6 program and discovered something surprising.

    When comparing strings, VB seems to compare always according Unicode values and not Ascii ones.

    For example:

    Code:
      If Chr$(128) > Chr$(146) Then
        MsgBox "128 is greater than 146!"
      End If
    When in fact it's comparing &H20AC (128 translated to Unicode) and &H2019 (146 translated to Unicode).

    Is there anything to do other than creating a function to compare Ansi?

    Note: I don't need this function, I have already written one. My question is just curiousity wheter there's any other way to do it.

  2. #2
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Comparing strings

    Chr$() says "take this long expression as an ANSI character code, make an ANSI String out of that, then convert it to a Unicode String and return it."

    ChrW$() is normally a more rational and faster choice, tough it wants a Unicode character code value.

    Maybe ChrB$() is what you really want here? I doubt it though. String values are normally Unicode, not ANSI.

  3. #3
    The Idiot
    Join Date
    Dec 2014
    Posts
    3,001

    Re: Comparing strings

    u can only use ascii 0-127 when comparing ascii

    128-255 are extended ascii code and can be anything depending on your locale.
    so those can be all messed up, as u notice 128 is above 146 but in another locale 146 can be above 128.
    but another way is to "not" do any chr$ but use safearray and take the "byte" value of the string directly when u compare.
    doing so, will not change the string into any locale, it will use the original locale when u created the string (if the string is located in a .dat file example)

    so if u plan on using unicode, u will need to work with locale as well.
    if u instead just want to use default letters A-Z, a-z those are within 0-127 are always the same in any locale and will always work.

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2021
    Posts
    5

    Re: Comparing strings

    Quote Originally Posted by dilettante View Post
    Chr$() says "take this long expression as an ANSI character code, make an ANSI String out of that, then convert it to a Unicode String and return it."

    ChrW$() is normally a more rational and faster choice, tough it wants a Unicode character code value.

    Maybe ChrB$() is what you really want here? I doubt it though. String values are normally Unicode, not ANSI.
    ChrB$ is what I need and didn't know it exists.

    It's an old app, as I have said, and it isn't working with Unicode at all.

    Thanks!

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2021
    Posts
    5

    Re: Comparing strings

    Quote Originally Posted by baka View Post
    u can only use ascii 0-127 when comparing ascii

    128-255 are extended ascii code and can be anything depending on your locale.
    so those can be all messed up, as u notice 128 is above 146 but in another locale 146 can be above 128.
    but another way is to "not" do any chr$ but use safearray and take the "byte" value of the string directly when u compare.
    doing so, will not change the string into any locale, it will use the original locale when u created the string (if the string is located in a .dat file example)

    so if u plan on using unicode, u will need to work with locale as well.
    if u instead just want to use default letters A-Z, a-z those are within 0-127 are always the same in any locale and will always work.
    Yes, I was taking the byte value in my function to binary safe compare. Anyway, changing Chr by ChrB is a cleaner solution. Thank you.

Tags for this Thread

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