diff options
Diffstat (limited to '')
-rw-r--r-- | utf16.cpp | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/utf16.cpp b/utf16.cpp new file mode 100644 index 0000000..eedb521 --- /dev/null +++ b/utf16.cpp | |||
@@ -0,0 +1,42 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <stdint.h> | ||
3 | |||
4 | void bitprint( uint16_t u ) | ||
5 | { | ||
6 | for( int i = 15; i >= 0; i-- ) | ||
7 | printf("%c", (u&(1<<i))?'1':'0'); | ||
8 | printf("\n"); | ||
9 | } | ||
10 | |||
11 | void bitprint( uint32_t u ) | ||
12 | { | ||
13 | for( int i = 31; i >= 0; i-- ) | ||
14 | printf("%c", (u&(1<<i))?'1':'0'); | ||
15 | printf("\n"); | ||
16 | } | ||
17 | |||
18 | void utoutf16( uint32_t in, uint16_t &outHi, uint16_t &outLo ) | ||
19 | { | ||
20 | outHi = (((in-0x10000)>>10)&0x3FF)| 0xD800u; | ||
21 | outLo = ((in-0x10000)&0x3FF)| 0xDC00u; | ||
22 | printf("0x%X == 0x%X, 0x%X\n", in, outHi, outLo ); | ||
23 | } | ||
24 | |||
25 | int32_t utf16tou( uint16_t hi, uint16_t lo ) | ||
26 | { | ||
27 | return (((uint32_t)hi&0x3FF)<<10 | lo&0x3FF)+0x10000; | ||
28 | } | ||
29 | |||
30 | int main() | ||
31 | { | ||
32 | bitprint( 0xD800u ); | ||
33 | bitprint( 0xDC00u ); | ||
34 | uint16_t hi, lo; | ||
35 | utoutf16( 0x1D11E, hi, lo ); // Cat face with wry smile | ||
36 | utoutf16( 0x10FFFD, hi, lo ); // Cat face with wry smile | ||
37 | utoutf16( 0x1F63C, hi, lo ); // Cat face with wry smile | ||
38 | bitprint( hi ); | ||
39 | bitprint( lo ); | ||
40 | printf("0x%X\n", utf16tou( hi, lo ) ); | ||
41 | return 0; | ||
42 | } | ||