在C / C ++中将ASCII转换为unicode字符串(无API)

I know I can convert ASCII to unicode strings using MultiByteToWideChar but I want an APIless solution. The only difference is that unicode is 2 bytes compared to ASCII, which is 1.

应该是以下内容,但它不起作用。

wchar_t* ascii_to_unicode(const char* ascii)
{
    if (!ascii)
        return nullptr;

    size_t len;
    wchar_t* unicode;

    len = strlen(ascii) * 2 + 1;
    if (!(unicode = reinterpret_cast<wchar_t*>(__malloc(len))))
        return nullptr;

    for (size_t i = 0; i < len; i++)
        *unicode++ = static_cast<wchar_t>(*ascii++);

    return unicode;
}

char* unicode_to_ascii(const wchar_t* unicode)
{
    if (!unicode)
        return nullptr;

    size_t len;
    char* ascii;

    len = wcslen(unicode) / 2 + 1;
    if (!(ascii = reinterpret_cast<char*>(__malloc(len))))
        return nullptr;

    for (size_t i = 0; i < len; i++)
        *ascii++ = static_cast<char>(*unicode++);

    return ascii;
}