Code-Beispiel
UTF8 in UTF32 Convertieren
Lizenz: | Erster Autor: | Letzte Bearbeitung: |
FBPSL | Sannaj | 19.11.2012 |
Diese Funktion nimmt einen zstring pointer auf einen UTF8-kodierten String entgegen
und gibt den Code point des ersten auftretenden Zeichens als 32 bit Integer zurück.
function UTF8toUTF32(utf8char as zstring ptr) as integer
dim as integer utf32char
select case (*utf8char)[0]
case is <= &h7F ' Ein-Byte-Sequence
utf32char = cint((*utf8char)[0])
case is <= &hBF ' Nicht start einer Sequence sondern Folgebyte
utf32char = -1 ' Also Fehlermeldung
case is <= &hDF ' Zwei-Byte-Sequence
' Das 2. Byte muss das Schema 0b10XX.XXXX haben.
if ((*utf8char)[1] and &hC0) <> &h80 then return -1
utf32char = cint((*utf8char)[1] and &h3F) ' Addiere 2. Byte
' Das 1. Byte hat die Form 0b110X.XXXX.
utf32char or= cint((*utf8char)[0] and &h1F) shl 6
case is <= &hEF ' Drei-Byte-Sequence
' Überprüfe das 2. und 3. Byte wie oben.
if ((*utf8char)[1] and &hC0) <> &h80 then return -1
if ((*utf8char)[2] and &hC0) <> &h80 then return -1
utf32char = cint((*utf8char)[2] and &h3F) ' Addiere 3. Byte
utf32char or= cint((*utf8char)[1] and &h3F) shl 6 ' Addiere 2. Byte
utf32char or= cint((*utf8char)[0] and &h1F) shl 12 ' Addiere 1. Byte
case is <= &hF7 ' Vier-Byte-Sequence
' Überprüfe die Folgebytes wie oben.
if ((*utf8char)[1] and &hC0) <> &h80 then return -1
if ((*utf8char)[2] and &hC0) <> &h80 then return -1
if ((*utf8char)[3] and &hC0) <> &h80 then return -1
utf32char = cint((*utf8char)[3] and &h3F) ' Addiere 4. Byte
utf32char or= cint((*utf8char)[2] and &h3F) shl 6 ' Addiere 3. Byte
utf32char or= cint((*utf8char)[1] and &h3F) shl 12 ' Addiere 2. Byte
utf32char or= cint((*utf8char)[0] and &h0F) shl 18 ' Addiere 1. Byte
case else ' Ungültige Möglichkeiten
utf32char = -1
end select
return utf32char
end function
Zusätzliche Informationen und Funktionen | |||||||
---|---|---|---|---|---|---|---|
|