diff --git a/libc/Makefile b/libc/Makefile
index 82557455..85a17d71 100644
--- a/libc/Makefile
+++ b/libc/Makefile
@@ -236,6 +236,7 @@ timespec/timespec.o \
time/strftime_l.o \
time/strftime.o \
time/timegm.o \
+wchar/btowc.o \
wchar/mbrlen.o \
wchar/mbrtowc.o \
wchar/mbsinit.o \
@@ -266,6 +267,7 @@ wchar/wcstoull.o \
wchar/wcstoul.o \
wchar/wcswidth.o \
wchar/wcsxfrm.o \
+wchar/wctob.o \
wchar/wcwidth.o \
wchar/wmemchr.o \
wchar/wmemcmp.o \
diff --git a/libc/include/wchar.h b/libc/include/wchar.h
index 0f7f7fb9..0198004c 100644
--- a/libc/include/wchar.h
+++ b/libc/include/wchar.h
@@ -120,7 +120,7 @@ typedef struct
struct tm;
-/* TODO: wint_t btowc(int); */
+wint_t btowc(int);
/* TODO: wint_t fgetwc(FILE*); */
/* TODO: wchar_t* fgetws(wchar_t* __restrict, int, FILE* __restrict); */
/* TODO: wint_t fputwc(wchar_t, FILE*); */
@@ -155,7 +155,7 @@ wchar_t* wcstok(wchar_t* __restrict, const wchar_t* __restrict, wchar_t** __rest
long wcstol(const wchar_t* __restrict, wchar_t** __restrict, int);
unsigned long wcstoul(const wchar_t* __restrict, wchar_t** __restrict, int);
size_t wcsxfrm(wchar_t* __restrict, const wchar_t* __restrict, size_t);
-/* TODO: int wctob(wint_t); */
+int wctob(wint_t);
wchar_t* wmemchr(const wchar_t*, wchar_t, size_t);
int wmemcmp(const wchar_t*, const wchar_t*, size_t);
wchar_t* wmemcpy(wchar_t* __restrict, const wchar_t* __restrict, size_t);
diff --git a/libc/wchar/btowc.cpp b/libc/wchar/btowc.cpp
new file mode 100644
index 00000000..edd1ad5a
--- /dev/null
+++ b/libc/wchar/btowc.cpp
@@ -0,0 +1,44 @@
+/*******************************************************************************
+
+ Copyright(C) Jonas 'Sortie' Termansen 2014.
+
+ This file is part of the Sortix C Library.
+
+ The Sortix C Library is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or (at your
+ option) any later version.
+
+ The Sortix C Library is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with the Sortix C Library. If not, see .
+
+ wchar/btowc.cpp
+ Single byte to wide character conversion.
+
+*******************************************************************************/
+
+#include
+#include
+#include
+#include
+
+extern "C" wint_t btowc(int ic)
+{
+ if ( ic == EOF )
+ return WEOF;
+ unsigned char uc = (unsigned char) ic;
+ char c = (char) uc;
+ mbstate_t ps;
+ memset(&ps, 0, sizeof(ps));
+ wchar_t wc;
+ int saved_errno = errno;
+ size_t status = mbrtowc(&wc, (const char*) &c, 1, &ps);
+ if ( status == (size_t) -1 || status == (size_t) -2 )
+ return errno = saved_errno, WEOF;
+ return (wint_t) wc;
+}
diff --git a/libc/wchar/wctob.cpp b/libc/wchar/wctob.cpp
new file mode 100644
index 00000000..14def1a8
--- /dev/null
+++ b/libc/wchar/wctob.cpp
@@ -0,0 +1,43 @@
+/*******************************************************************************
+
+ Copyright(C) Jonas 'Sortie' Termansen 2014.
+
+ This file is part of the Sortix C Library.
+
+ The Sortix C Library is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or (at your
+ option) any later version.
+
+ The Sortix C Library is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with the Sortix C Library. If not, see .
+
+ wchar/wctob.cpp
+ Wide-character to single-byte conversion.
+
+*******************************************************************************/
+
+#include
+#include
+#include
+#include
+#include
+
+extern "C" int wctob(wint_t wc)
+{
+ mbstate_t ps;
+ memset(&ps, 0, sizeof(ps));
+ char mb[MB_CUR_MAX];
+ int saved_errno = errno;
+ size_t status = wcrtomb(mb, wc, &ps);
+ if ( status == (size_t) -1 )
+ return errno = saved_errno, EOF;
+ if ( 2 <= status )
+ return EOF;
+ return (int) (unsigned char) mb[0];
+}