How to convert utf8 to utf16 string?

Discussion forum about all things Team Developer 5.x and 6.x
hotenise

How to convert utf8 to utf16 string?

Post by hotenise » 28 Oct 2007, 02:50

How to convert utf8 to utf16 string?



thanks.

Dave Rabelink
Founder/Site Admin
Founder/Site Admin
Netherlands
Posts: 3421
Joined: 24 Feb 2017, 09:12
Location: Gouda, The Netherlands

Re: How to convert utf8 to utf16 string?

Post by Dave Rabelink » 28 Oct 2007, 08:27

hotenise wrote:How to convert utf8 to utf16 string?

thanks.




SalStrToWideChar

Syntax

bOk = SalStrToWideChar ( strInput,strOutput,nEncoding )

Description

Converts an multibyte string to a unicode string.

Parameters

strInput String. The string to convert.

strOutput String. The output string.

nEncoding Number. The encoding to use. The following pre-defined number constants have been defined:

ENC_ANSI

ENC_MACCP

ENC_OEMCP

ENC_UTF7

ENC_UTF8

Return Value

bOk is TRUE if the function succeeds and FALSE if it fails.

See Also

SalStrToMultiByte











SalStrToMultiByte

Syntax

bOk = SalStrToMultiByte ( strInput,strOutput,nEncoding )

Description

Converts a unicode string to a multibyte string.

Parameters

strInput String. The string to convert.

strOutput String. The output string.

nEncoding Number. The encoding to use. The following pre-defined number constants have been defined:

ENC_ANSI

ENC_MACCP

ENC_OEMCP

ENC-UTF7

ENC_UTF8

Return Value

bOk is TRUE if the function succeeds and FALSE if it fails.

See Also

SalStrToWideChar
Regards,
Dave Rabelink

Image
Articles and information on Team Developer Tips & Tricks Wiki
Download samples, documents and resources from TD Sample Vault
Videos on TDWiki YouTube Channel

Jean-Marc Gemperle

Re: How to convert utf8 to utf16 string?

Post by Jean-Marc Gemperle » 28 Oct 2007, 15:51

Hi Dave,



Many thanks for your answer. Here in Holland for us weather is not thag good today ;-) so wanted to check if one can convert from UFT16 to UTF8 and BACK as indeed your answer is the answer I would have done too...Unfortunatelly there seems to be couple issues



1)SalStrToMultiByte/SalStrToWideChar will always fail returning false when ENC_UTF8 is used, I believe this is because internally we use MB_PRECOMPOSED and we should not according to MSDN when using UTF8. So I did a testcase checking all that and by calling MultiByteToWideChar/WideCharToMultiByte WINDOWS API I can get it to work...so I will log an issue for ENC_UTF8 not working as far as I can see



2)The other issues is that I don't see why INTERNALY one would like to have a UTF8 buffer but of course passing it to some API or especially writing an UTF8 file is what people would like to do.... Now TD 5.1 currently only support wrinting ANSI file ( that is the default), this mean TD make a conversion using the local code page to convert unicode to ansi transparently. If you want to have a UNICODE file you can use OF_UTF16 flag in SalFileOpen() and this is working fine. The question is how to generate a UTF-8 file (like notpad does).... Basically I could not. MultiByteToWideChar works fine with UTF8 and the call is succesfull but in the buffer I get back from this call I seem to have always UTF16 encoded string...I try to save that buffer as binary and I do get UTF32...without the BOM ( I guess that is expected not having the BOM)



find attached my test with comments in the source...



Cheers

Jean-Marc
You do not have the required permissions to view the files attached to this post.

Return to “General Discussion TD 5.x and 6.x”

Who is online

Users browsing this forum: [Ccbot] and 0 guests