Analyze standard and non-standard characters via Byte
Array
This example uses OP's first line string "♫muz♫" of variable s
assigned to a Byte
array which allows to analyze each character code represented by two bytes per character. This 2-byte architecture allows to detect non-standard characters, get their decimal or hexadecimal values (~~> cf. ChrW
and AscW
function). Furthermore this example isolates the standard characters in a final s
variable. Feel free to modify code in any wanted direction :-)
Hint
A very helpful site to search non-standard characters by direct Input can be found at https://unicode-table.com/en/blocks/
Sub DedectNonStandardSymbols()
' Meth: analyze a Byte-array consisting of 2 bytes with a pair of character codes;
' e.g. code sequence 96 followed by 0 represents the standard character "a"
' Note: VBA handles strings in Unicode encoded as UTF-16, so
' each ‘character’ of the string corresponds at least to a 16 bit WORD (2 bytes) of memory.
' Hint: For details about Windows' Little Endian architecture
' where the lowest significant byte appears first in memory, followed by the most significant byte at the next address.
' see http://support.microsoft.com/kb/102025
Dim by() As Byte, by2() As Byte, s As String
Dim i&, ii&, dec&
' build example string (cf. 1st example string in OP)
s = ChrW(&H266B) & "muz" & ChrW(&H266B) ' beamed eighth notes surrounding the standard characters "muz"
' get byte sequences
by = s: ReDim by2(0 To UBound(by))
' loop through array and detect non standard characters
For i = LBound(by) To UBound(by) Step 2
dec = by(i + 1) * 16 * 16 + by(i) ' 2nd word (byte)
Debug.Print i / 2 + 1, by(i) & ", " & by(i + 1), _
"dec " & dec & "/hex &H" & WorksheetFunction.Dec2Hex(Format(dec, "0")), _
"=> """ & ChrW(dec) & """" & IIf(by(i + 1) = 0, "", " (non-standard char)")
If by(i + 1) = 0 Then
by2(ii) = by(i): by2(ii + 1) = 0: ii = ii + 2
End If
Next i
ReDim Preserve by2(0 To ii - 1)
s = by2
Debug.Print "String without non-standard chars: """ & s & """"
End Sub
Example output in VBE's immediate window
1 107, 38 dec 9835/hex &H266B => "?" (non-standard char)
2 109, 0 dec 109/hex &H6D => "m"
3 117, 0 dec 117/hex &H75 => "u"
4 122, 0 dec 122/hex &H7A => "z"
5 107, 38 dec 9835/hex &H266B => "?" (non-standard char)
String without non-standard chars: "muz"