Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1502

AscU unicode equivalent of Asc

$
0
0
This proposal introduces the AscU utility, a variant of the Asc function designed to retrieve the Unicode code point of any character.

While the standard AscW function is often used for this purpose, it is frequently misunderstood and misused. In practice, AscW returns a signed integer (ranging from -32,768 to 32,767). Some developers attempt to use it for UTF-8 conversion, and tests are often misinterpreted ex:

Code:

      Select Case  AscW(Mid(Txt, i, 1))  'AscW may returns a negative value
        Case Is < 128:
           
        Case Is < 2048:
         
      ...

AscU:


Code:

Function AscU(s As String, aPos) As Long
Dim h As Long, l As Long
  h = AscW(Mid(s, aPos, 1)) And &HFFFF&
  aPos = aPos + 1
  If (h >= &HD800&) And (h <= &HDBFF&) Then
    l = AscW(Mid(s, aPos, 1)) And &HFFFF&
    aPos = aPos + 1
    If (l >= &HDC00&) And (l <= &HDFFF&) Then
        AscU = (h And &H3FF&) * 1024
        AscU = (AscU Or (l And &H3FF&)) + &H10000
        Exit Function
    End If
  End If
  AscU = h
End Function

Example:
Code:

Private Sub test()
Dim i As Long, s As String
i = 1
s = TextBox1 ' Unicode office TextBox control
While i <= Len(s)
  Debug.Print Hex(AscU(s, i))
Wend

End Sub

Hope this works.

Viewing all articles
Browse latest Browse all 1502

Latest Images

Trending Articles



Latest Images