char* to UTF-8 (translating java to c++)

Hi,

I am having trouble translating the following java code to c++:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 String readString(int size, int len, String charset) throws IOException
{
    int length = (size > 0 ? size : len);
    byte[] bytes = new byte[length];
    this.stream.read(bytes);
    length = (len >= 0 ? len : size);
   
    try
    {
        return new String(new String(bytes, 0, length, charset).getBytes("UTF-8"), "UTF-8");
    }
    catch(Throwable e)
    {
        e.printStackTrace();
    }
    return new String(bytes, 0, length);
} 


this is my c++ code so far:

1
2
3
4
5
6
7
8
9
??? Test::readString(int size, int len)
{
    int length = (size > 0 ? size : len);
    unsigned char* bytes = new unsigned char[length];

    stream.read(reinterpret_cast<char*>(bytes), length);


} 


After reading from the stream the char* should be converted to an UTF-8 string. As far as I know wchar_t is Unicode but with 16-bit, whereas a char* uses 8-bit ASCII.

I hope anyone has an idea how to convert this to UTF-8.

Any hint is appreciated!

http://www.cplusplus.com/forum/general/7142/#msg33079

EDIT: Alternatively, Google iconv.
Last edited on
Topic archived. No new replies allowed.