-
Notifications
You must be signed in to change notification settings - Fork 0
/
utf8towcs.c
77 lines (70 loc) · 1.22 KB
/
utf8towcs.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
/*
* WESLEY'S NT
*
* Title: UTF-8 to Wide char
* Author: Wesley
* License: GPLv3
*
* Dependencies: C library
*
*/
#include <stdio.h>
#include <stdlib.h>
wchar_t* utf8stowcs(char*str,int len) // len = character count - extra code bytes
{
wchar_t*out = (wchar_t*)malloc((len+1)<<1);
out[len] = 0;
wchar_t tmp;
char tmp2;
int h = 0; // :P
for (int i = 0; i < len; i++)
{
if (!(str[h] & 0x80))
{
out[i] = str[h];
//puts("0");
}
else
{
char j, k = 0;
tmp2 = str[h];
// use bitstream |:/
k++;
for (j = 1; j < 6; j++)
{
if (!(tmp2 >> (7-j) & 1))
break;
k++;
}
tmp = str[h] & (255 >> k);
//printf("%u %02X\n %02X\n", k, str[h], tmp);
for (j = 0; j < k-1; j++)
{
tmp <<= 6;
//printf("| %02X\n", str[++h] & 0b00111111);
tmp |= (str[++h] & 0b00111111);
}
//printf("%04X\n",tmp);
//putwchar(tmp);
//puts("");
out[i] = tmp;
}
h++;
}
return out;
}
#if 1
#include "ez.c"
_start()
{
//printf("Hello, World!\n");
char*utf8str = load("utf8test.txt");
wchar_t*newwide = utf8stowcs(utf8str,27);
//printf("%u", wcslen(newwide));
//puts("");
FILE* widetest =
fopen("widetest.txt","wb");
fwprintf(widetest,newwide);
fclose(widetest);
}
#endif