|
-
Jul 8th, 2023, 11:29 AM
#1
Thread Starter
New Member
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.
-
Jul 8th, 2023, 11:48 AM
#2
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.
-
Jul 8th, 2023, 12:12 PM
#3
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.
-
Jul 10th, 2023, 03:55 AM
#4
Thread Starter
New Member
Re: Comparing strings
 Originally Posted by dilettante
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!
-
Jul 10th, 2023, 04:04 AM
#5
Thread Starter
New Member
Re: Comparing strings
 Originally Posted by baka
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|