-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutf16_to_utf8.c
93 lines (83 loc) · 2.08 KB
/
utf16_to_utf8.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <stdio.h>
int main(void){
unsigned int ch1 = 0 ,ch2 = 0,ch3 = 0,ch4 = 0;
unsigned int code;
unsigned int codeh1,codeh2;
unsigned int c,b ,b1,b2, a1, a2;
ch1 = getchar();
while (ch1 != EOF) {
code = 0;
codeh1 = 0;
c = 0;
b = 0;
b1 = 0;
b2 = 0;
a1 = 0;
a2 = 0;
ch2 = getchar();
code = (ch1 << 8) | ch2;
if (!((code >= 0x0000 && code <= 0xD7FF) | (code >= 0xE000 && code <= 0xFFFF))) {
code = 0;
ch3 = getchar();
ch4 = getchar();
a1 = (ch1 << 8) | ch2;
a2 = (ch3 << 8) | ch4;
if ((a1 >= 0xD800 && a1 <= 0xDBFF) & (a2 >= 0xDC00 && a2 <= 0xDFFF)) { // Is a 4 bytes in Utf-16.
b1 = a1 - 0xD800;
b2 = a2 - 0xDC00;
b = (b1 << 10) | b2;
c = b + 0x010000;
code = c;
}
else {
printf("ERROR\n");
}
}
///// now we check code and make it utf-8
if (code >= 0x0000 && code <= 0x007F) { // we must make it 1 byte utf-8
codeh1 |= code;
putchar(codeh1);
}
else if (code >= 0x0080 && code <= 0x07FF) { // we must make it 2 byte utf-8
codeh1 = (code >> 6) | 0xC0;
codeh2 = code << 26;
codeh2 >>= 26;
codeh2 |= 0x80;
putchar(codeh1);
putchar(codeh2);
}
else if (code >= 0x0800 && code <= 0xFFFF) { // we must make it 3 byte utf-8
codeh1 = (code >> 12) | 0xE0;
codeh2 = code << 20;
codeh2 >>= 26;
codeh2 |= 0x80;
putchar(codeh1);
putchar(codeh2);
codeh1 = code << 26;
codeh1 >>= 26;
codeh1 |= 0x80;
putchar(codeh1);
}
else if (code >= 0x10000 && code <= 0x10FFFF) { // we must make it 4 byte utf-8
codeh1 = (code >> 11);
codeh1 |= 0xF0;
codeh2 = code << 14;
codeh2 >>= 26;
codeh2 |= 0x80;
putchar(codeh1);
putchar(codeh2);
codeh1 = code << 20;
codeh1 >>= 26;
codeh1 |= 0x80;
codeh2 = code << 26;
codeh2 >>= 26;
codeh2 |= 0x80;
putchar(codeh1);
putchar(codeh2);
}
ch1 = getchar();
}
return 0;
}
// sdi1500129
// Editor : Panagiotis Petropoulakis