Advertisement
6_2008-2009 Debugging and Error Handling #216569

How to tell if a String is Unicode

Identifies whether a string is encoded in Unicode.

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
Upload
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
 Next
 Return bIsUnicode
 End Function
Original Comments (3)
Recovered from Wayback Machine