Pregunta

The question I have is quite simple, but I couldn't find a solution so far:

How can I convert a UTF8 encoded string to a latin1 encoded string in C++ without using any extra libs like libiconv?

Every example I could find so far is for latin1 to UTF8 conversion?

¿Fue útil?

Solución

typedef unsigned value_type;

template <typename Iterator>
size_t get_length (Iterator p)
{
    unsigned char c = static_cast<unsigned char> (*p);
    if (c < 0x80) return 1;
    else if (!(c & 0x20)) return 2;
    else if (!(c & 0x10)) return 3;
    else if (!(c & 0x08)) return 4;
    else if (!(c & 0x04)) return 5;
    else return 6;
}

template <typename Iterator>
value_type get_value (Iterator p)
{
    size_t len = get_length (p);

    if (len == 1)
    return *p;

    value_type res = static_cast<unsigned char> (
                                    *p & (0xff >> (len + 1)))
                                     << ((len - 1) * 6);

    for (--len; len; --len)
        res |= (static_cast<unsigned char> (*(++p)) - 0x80) << ((len - 1) * 6);

    return res;
}

This function will return the unicode code point at p. You can now convert a string using

for (std::string::iterator p = s_utf8.begin(); p != s_utf8.end(); ++p)
{
     value_type value = get_value<std::string::iterator&>(p));
     if (value > 0xff)
         throw "AAAAAH!";
     s_latin1.append(static_cast<char>(value));
}

No guarantees, the code is quite old :)

Otros consejos

Here is a version of filmor's answer that I wrote for my purposes. A bit more readable, probably a bit slower. I didn't need the template stuff since I was always dealing with char *, and in my case I wanted to replace non-Latin1 character's with _. Just in case it helps someone:

int GetUtf8CharacterLength( unsigned char utf8Char )
{
    if ( utf8Char < 0x80 ) return 1;
    else if ( ( utf8Char & 0x20 ) == 0 ) return 2;
    else if ( ( utf8Char & 0x10 ) == 0 ) return 3;
    else if ( ( utf8Char & 0x08 ) == 0 ) return 4;
    else if ( ( utf8Char & 0x04 ) == 0 ) return 5;

    return 6;
}

char Utf8ToLatin1Character( char *s, int *readIndex )
{
    int len = GetUtf8CharacterLength( static_cast<unsigned char>( s[ *readIndex ] ) );
    if ( len == 1 )
    {
        char c = s[ *readIndex ];
        (*readIndex)++;

        return c;
    }

    unsigned int v = ( s[ *readIndex ] & ( 0xff >> ( len + 1 ) ) ) << ( ( len - 1 ) * 6 );
    (*readIndex)++;
    for ( len-- ; len > 0 ; len-- )
    {
        v |= ( static_cast<unsigned char>( s[ *readIndex ] ) - 0x80 ) << ( ( len - 1 ) * 6 );
        (*readIndex)++;
    }

    return ( v > 0xff ) ? 0 : (char)v;
}

// overwrites s in place
char *Utf8ToLatin1String( char *s )
{
    for ( int readIndex = 0, writeIndex = 0 ; ; writeIndex++ )
    {
        if ( s[ readIndex ] == 0 )
        {
            s[ writeIndex ] = 0;
            break;
        }

        char c = Utf8ToLatin1Character( s, &readIndex );
        if ( c == 0 )
        {
            c = '_';
        }

        s[ writeIndex ] = c;
    }

    return s;
}

Test code:

char s2[ 256 ] = "lif\xc3\xa9 is b\xc3\xa9tt\xc3\xa9r with acc\xc3\xa9nts";
Utf8ToLatin1String( s2 );

latin1 (aka ISO-8859-1) defines the first 256 code points of Unicode. Thus, in UTF-8, if your character is 8 bits, then it will exactly map to the latin1 equivalent. If it's more than 8 bits in length, then there is no correspondent within latin1 and you should map it to some "unknown character" (e.g., \0 or ?).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top