VB icon

How to tell if a String is Unicode

Submitted on: 1/4/2015 10:19:00 PM
By: VBRocks (from psc cd)  
Level: Advanced
User Rating: By 2 Users
Compatibility: VB.NET
Views: 4845
     Identifies whether a string is encoded in Unicode.
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
// Name: How to tell if a String is Unicode
// Description:Identifies whether a string is encoded in Unicode.
// By: VBRocks (from psc cd)
// Inputs:String.
// Returns:Boolean: True or False
// Assumes:Call the function passing it a string and it will return True if the String is encoded in Unicode, False if not.
// Side Effects:None.

Private Function IsStringUnicode(ByVal str As String) As Boolean
 'Set it to False by default
 Dim bIsUnicode As Boolean = False
 'Setup Encodings
 Dim EncDefault As System.Text.Encoding = System.Text.Encoding.GetEncoding(0) 'String Default Encoding
 Dim EncUnicode As System.Text.Encoding = System.Text.Encoding.Unicode'Encoding
 Dim bitesDefault As Byte() = EncDefault.GetBytes(str) 'Get the bytes of the string using the string's default encoding
 Dim bitesUnicode As Byte() = EncUnicode.GetBytes(str)
 Dim charsDefault As Char() = EncDefault.GetChars(bitesDefault) 'Get the characters of the default string
 Dim charsUnicode As Char() = EncUnicode.GetChars(bitesUnicode) 'Get the characters in unicode
 'Loop through all the characters and see if they all match.
 ' if any do not match, then it's unicode
 For i As Integer = 0 To charsDefault.Length - 1
If charsDefault(i) <> charsUnicode(i) Then
bIsUnicode = True
'we found one that doesn't match, so exit the loop
Exit For
End If
 Return bIsUnicode
 End Function

Other 5 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)

To post feedback, first please login.