-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_wctomb.c
47 lines (44 loc) · 1.48 KB
/
ft_wctomb.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_wctomb.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ewilliam <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/04 15:56:56 by ewilliam #+# #+# */
/* Updated: 2017/03/04 15:56:58 by ewilliam ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_wctomb(char *s, wchar_t wchar)
{
char *tmp;
tmp = s;
if (wchar <= 0x007F)
{
*tmp = wchar;
return (1);
}
else if (wchar <= 0x07FF)
{
*tmp++ = (wchar >> 6) + 0xC0;
*tmp = (wchar & 0x3F) + 0x80;
return (2);
}
else if (wchar <= 0xFFFF)
{
*tmp++ = (wchar >> 12) + 0xE0;
*tmp++ = ((wchar >> 6) & 0x3F) + 0x80;
*tmp++ = (wchar & 0x3F) + 0x80;
return (3);
}
else if (wchar <= 0x10FFFF)
{
*tmp++ = (wchar >> 18) + 0xF0;
*tmp++ = ((wchar >> 12) + 0xE0) + 0x80;
*tmp++ = ((wchar >> 6) & 0x3F) + 0x80;
*tmp++ = (wchar & 0x3F) + 0x80;
return (4);
}
return (-1);
}