Board index » cppbuilder » WideCharToString() make wrong conversion between different OS.

WideCharToString() make wrong conversion between different OS.

I have problem...about string conversion.

I have Korean Window 2000 and Japanese Window 2000, and I have to send and
receive all kind of language strings.

So..I decided to use WideString to send and receive strings.

I sended some Korean string converted to WideString on Korean Window 2000
to Japanese Window 2000. And I converted them to AnsiStrig using
WideCharToString() function.

But...it gave me wrong conversion.

here's source code on each side.
Please check this out.

[On Send Side]
     AnsiString Source = "?????";
        wchar_t *buf = new wchar_t[Source.Length()];
        StringToWideChar(Source, buf, Source.Length());
        int result = ClientSocket1->Socket->SendBuf(buf, Source.Length())

[On Receive Side]

        int length = Socket->ReceiveLength();
        wchar_t *buf = new wchar_t[length];
        Socket->ReceiveBuf(buf, length);

        AnsiString Result = WideCharToString(buf);

 

Re:WideCharToString() make wrong conversion between different OS.


Quote
Kwon Hyuk-min wrote:
> I have problem...about string conversion.

You could narrow down the problem a bit by not sending and receiving the
buffer.

Is the conversion the same if you try:

        AnsiString Source = "?????";
         wchar_t *buf = new wchar_t[Source.Length()];
         StringToWideChar(Source, buf, Source.Length());
        AnsiString Result = WideCharToString(buf);
        delete [] buf;

Are you sure that buf is null terminated ?

 > But...it gave me wrong conversion.

What was wrong on it ?

Hans.

Hans.

Re:WideCharToString() make wrong conversion between different OS.


Quote
"Kwon Hyuk-min" <lepiani...@hitel.net> wrote in message

news:Xns939C7A0C0C53lepianistehitelnet@207.105.83.65...

Quote
> I sended some Korean string converted to WideString on
> Korean Window 2000 to Japanese Window 2000. And I
> converted them to AnsiStrig using WideCharToString() function.

First off, your socket are sending and receiving the data incorrectly to
begin with.  You are converting the sockets to wchar_t, which is 2 bytes per
character, not 1 byte.  But sockets work at the byte level.  You are only
transmitting half of the string data because you are using the original
AnsiString's Length(), which is the number of ANSI characters, not the
number of raw bytes for the transmitted buffer.

When using StringToWideChar(), you should be using the AnsiString's
WideCharBufSize() method instead of calculating the size of the buffer.
However, why not just assign the AnsiString directly to a WideString, and
vice versa?  AnsiString and WideString automatically handle the necessary
conversions internally for you:

    AnsiString asSource = "?????";
    WideString wsSource = asSource;
    int length = (wsSource.Length()*sizeof(w_char_t));
    ClientSocket1->Socket->SendBuf(&length, sizeof(int));
    int result = ClientSocket1->Socket->SendBuf(wsSource.c_bstr(), length);

    int length = 0;
    while( Socket->ReceiveLength() < sizeof(int) )
Application->ProcessMessages();
    Socket->ReceiveBuf(&length, sizeof(int));
    while( Socket->ReceiveLength() < length )
Application->ProcessMessages();
    WideString wsBuf;
    wsBuf.SetLength(length/sizeof(wchar_t));
    Socket->ReceiveBuf(buf.c_bstr(), length);
    AnsiString Result = wsBuf;

Another alternative you may want to consider is to use WideString for the
original strings and then convert them to UTF-8 for just the transmission
part (assuming you have BCB6, that is):

    WideString wsSource = "?????";
    AnsiString asSource = UTFEncode(wsSource);
    int length = asSource.Length();
    ClientSocket1->Socket->SendBuf(&length, sizeof(int));
    int result = ClientSocket1->Socket->SendBuf(asSource.c_str(), length);

    int length = 0;
    while( Socket->ReceiveLength() < sizeof(int) )
Application->ProcessMessages();
    Socket->ReceiveBuf(&length, sizeof(int));
    while( Socket->ReceiveLength() < length )
Application->ProcessMessages();
    AnsiString asBuf;
    asBuf.SetLength(length);
    Socket->ReceiveBuf(buf.c_str(), length);
    WideString Result = UTFDecode(asBuf);

Gambit

Other Threads