FireBirdLib - Topfield TMS PVR TAP Programming Library
UTF8ToUTF32.c
Go to the documentation of this file.
1#include "libFireBird.h"
2
3dword UTF8ToUTF32(const byte *UTF8Character, byte *BytesPerChar)
4{
6
7 byte c1, c2, c3, c4;
8
9 if(BytesPerChar) *BytesPerChar = 1;
10 if(!UTF8Character)
11 {
12 TRACEEXIT();
13 return 0;
14 }
15
16 //0xxx xxxx
17 c1 = UTF8Character[0];
18 if((c1 & 0x80) == 0x00)
19 {
20 TRACEEXIT();
21 return c1;
22 }
23
24 //110x xxxx 10xx xxxx
25 c2 = UTF8Character[1];
26 if(((c1 & 0xe0) == 0xc0) &&
27 ((c2 & 0xc0) == 0x80))
28 {
29 if(BytesPerChar) *BytesPerChar = 2;
30
31 TRACEEXIT();
32 return ((c1 & 0x1f) << 6) | (c2 & 0x3f);
33 }
34
35 //1110 xxxx 10xx xxxx 10xx xxxx
36 c3 = UTF8Character[2];
37 if(((c1 & 0xf0) == 0xe0) &&
38 ((c2 & 0xc0) == 0x80) &&
39 ((c3 & 0xc0) == 0x80))
40 {
41 if(BytesPerChar) *BytesPerChar = 3;
42
43 TRACEEXIT();
44 return ((c1 & 0x0f) << 12) | ((c2 & 0x3f) << 6) | (c3 & 0x3f);
45 }
46
47 //1111 0xxx 10xx xxxx 10xx xxxx 10xx xxxx
48 c4 = UTF8Character[3];
49 if(((c1 & 0xf8) == 0xf0) &&
50 ((c2 & 0xc0) == 0x80) &&
51 ((c3 & 0xc0) == 0x80) &&
52 ((c4 & 0xc0) == 0x80))
53 {
54 if(BytesPerChar) *BytesPerChar = 4;
55
56 TRACEEXIT();
57 return ((c1 & 0x07) << 18) | ((c2 & 0x3f) << 12) | ((c3 & 0x3f) << 6) | (c4 & 0x3f);
58 }
59
60 //Malformed packet, return that character
61 TRACEEXIT();
62 return c1;
63}
dword UTF8ToUTF32(const byte *UTF8Character, byte *BytesPerChar)
Definition: UTF8ToUTF32.c:3
#define TRACEEXIT()
Definition: libFireBird.h:1244
#define TRACEENTER()
Definition: libFireBird.h:1243