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