Sindup:isUtf8

De Hegyd Doc.

(Différences entre les versions)
Vanina Lebouvier (discuter | contributions)
(Page créée avec « ''' En C ''' for (i = 0; (c = utf[i]); i+= nb, nb = 0) { if( (c & 0x80) != 0x80) // 1 octet nb = 1; else if ((c & 0xE0) == 0xC0) // 2 octets nb = 2; else if ((c … »)
Modification suivante →

Version du 31 janvier 2012 à 10:26

En C

for (i = 0; (c = utf[i]); i+= nb, nb = 0) {
 if( (c & 0x80) != 0x80) // 1 octet
  nb = 1;
 else if ((c & 0xE0) == 0xC0) // 2 octets
  nb = 2;
 else if ((c & 0xF0) == 0xE0) // 3 octets
  nb = 3;
 else if ((c & 0xF8) == 0xF0) // 4 octets
  nb = 4;
 else if ((c & 0xFC) == 0xF8) // 5 octets
  nb = 5;
 else if ((c & 0xFE) == 0xFC) // 6 octets
  nb = 6;
 else
  return 1;      // N'est pas de l'utf8
 for( j = 1; j < nb && (c = utf[i+j]); j++) {
  if( (c & 0xC0) != 0x80)
   return 1;
 }
}


Caractères UTF8 sur :

  • 1 octet :
0xxx xxxx
c1 & 0x80 != 0x80
  • 2 octets :
110x xxxx - 10xx xxxx
c1 & 0xE0 == 0xC0
c2 & 0xC0 == 0x80
  • 3 octets :
1110 xxxx - 10xx xxxx - 10xx xxxx
c1 & 0xF0 == 0xE0
c2 & 0xC0 == 0x80
c3 & 0xC0 == 0x80
  • 4 octets :
1111 0xxx - 10xx xxxx - 10xx xxxx - 10xx xxxx
c1 & 0xF8 == 0xF0
c2 & 0xC0 == 0x80
c3 & 0xC0 == 0x80
c4 & 0xC0 == 0x80
  • 5 octets :
1111 10xx - 10xx xxxx - 10xx xxxx - 10xx xxxx - 10xx xxxx
c1 & 0xFC == 0xF8
c2 & 0xC0 == 0x80
c3 & 0xC0 == 0x80
c4 & 0xC0 == 0x80
c5 & 0xC0 == 0x80
  • 6 octets :
1111 110x - 10xx xxxx - 10xx xxxx - 10xx xxxx - 10xx xxxx - 10xx xxxx
c1 & 0xFE == 0xFC
c2 & 0xC0 == 0x80
c3 & 0xC0 == 0x80
c4 & 0xC0 == 0x80
c5 & 0xC0 == 0x80
c6 & 0xC0 == 0x80

http://sebsauvage.net/python/charsets_et_encoding.html