[RESOLVED] Unicode Validation

Hai,
What is the VB Function to check a piece of string is unicode or Ascii ?
Thanks
[102 byte] By [NotepadGuru] at [2007-11-20 10:22:42]
# 1 Re: [RESOLVED] Unicode Validation
There really isn't one. All strings, internally to VB, are unicode. Here is a simple function I have used from time to time:
Private Function isStringANSI(inText As String) As Boolean

' simple test to determine if passed string is ANSI-like or not.
' In other words, does it contain unicode characters.

Dim tArray() As Byte
Dim X As Long

If inText = vbNullString Then

isStringANSI = True

Else

tArray = inText
For X = LBound(tArray) + 1 To UBound(tArray) Step 2
If Not tArray(X) = 0 Then Exit For
Next

isStringANSI = (X > UBound(tArray))

End If

End Function
LaVolpe at 2007-11-9 19:35:03 >
# 2 Re: [RESOLVED] Unicode Validation
LaVofe,
If you dont mined, I might need an explanation to your code, i did not undestand what you compare against what :)
NotepadGuru at 2007-11-9 19:36:09 >
# 3 Re: [RESOLVED] Unicode Validation
No problem. The line of code "tArray = inText" moves the passed string into an array. This method preserves unicode if string is unicode. Regardless the string passed in is always 2 bytes per character. To validate: try Debug.Print UBound(tArray)+1 = Len(inText)*2

Now, we loop thru every other byte to see if it is zero or not, starting with the 2nd byte. When a string has unicode characters, at least one of those "every other bytes" will be non-zero. If all are zero, then the string contains no unicode characters. That is the final test. If the loop got thru the entire stirng without exiting early, no unicode characters, otherwise the loop exits on the first hit and string contains unicode characters. Does that explain it? If not, let me know.
LaVolpe at 2007-11-9 19:37:02 >
# 4 Re: [RESOLVED] Unicode Validation
Owsome explanation !!! Sorry, no more question. Let me Rate your post.
NotepadGuru at 2007-11-9 19:38:07 >
# 5 Re: [RESOLVED] Unicode Validation
I may have mislead you, by mistake. There is an API called IsTextUnicode that should do what the routine I posted does, plus more. It is an NT-only API (i.e., can't be used on Win98,WinME,Win95). You can review this MSDN page (http://msdn2.microsoft.com/en-us/library/ms776445.aspx) and see if you want to play with it or not. If you read the description of some of the possible final parameter values, it is kinda strange to see MSDN terms like "the text is probably unicode".
LaVolpe at 2007-11-9 19:39:12 >
# 6 Re: [RESOLVED] Unicode Validation
I may have mislead you, by mistake."
i did not think so, coz i learned somthing new. that is the 2byte unicode byte attray thing. i am really happy. now let me try the api. :D
NotepadGuru at 2007-11-9 19:40:11 >