Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: CBXE7 - char16_t, wchar_t, WideChar, UnicodeString.


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 2 - Last Post: Oct 1, 2014 6:41 PM Last Post By: GAI CHEW KAI
GAI CHEW KAI

Posts: 117
Registered: 7/25/14
CBXE7 - char16_t, wchar_t, WideChar, UnicodeString.  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 30, 2014 8:27 PM
...I am using CBXE7, may be I used to VC++, often use wchar_t as parameter passing such as:

__inline int ___fc (__in wchar_t* pwcPath)
{
bool bResult;
int iResult;

#if defined (__MSWINDOWS__)
iResult = _waccess( pwcPath, 0 );
if( !iResult )
return 0;
#endif // __MSWINDOWS__

#if defined (__ANDROID__)
//
// How to convert pwcPath to UnicodeString ?
//
bResult = FileExists( // expects UnicodeString );
if( bResult )
return 0;

bResult = DirectoryExists( // expects UnicodeString );
if( bResult )
return 0;
#endif // __ANDROID__

return -1;
}

QUESTIONS:

1. Is there a way to convert from wchar_t to UnicodeString or vice versa ?

2. Why I need to dynamically cast for __fc( (wchar_t*)Textbox->Text.w_str() ) where .w_str() returns as WideChar* and WideChar is typedef of wchar_t under help documentation ?

( e.g From the Help document: System.WideCharFrom RAD Studio API Documentation .... typedef wchar_t WideChar; )

3. How come _access() not even implemented in FMX for Android ?

4. If I were to use UnicodeString.sprintf(), is there a format specifier for wchar_t* since there is no %ls and %ls is not supported under MacOS ?

5. UTF8String().c_str() is it the same as char* in FMX ?

6. If statement 5 is TRUE, then what causes it can hold Unicode character after converted from UTF8String() ?

7. I often used wchar_t, and when came to CBXE, I have to use UnicodeString as standard string data handling, but there are time when legacy written in VC++, I often used wchar_t, therefore, really need to know is there a way or any resource that can do string conversion between wchar_t and UnicodeString. If there is any website or link, is greatly appreciated.

8. What is the different between char16_t and wchar_t , since both carried 16 bits per character ?

Please advise.

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: CBXE7 - char16_t, wchar_t, WideChar, UnicodeString. [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 1, 2014 10:35 AM   in response to: GAI CHEW KAI in response to: GAI CHEW KAI
GAI wrote:

...I am using CBXE7, may be I used to VC++, often use wchar_t as
parameter passing such as:

If you want to be compatible with multiple platforms without using ifdefs
around your variable/parameter declarations, use System::Char instead. It
is a typedef for wchar_t on Windows and char16_t on other platforms.

// How to convert pwcPath to UnicodeString ?

Just assign it directly. UnicodeString has constructors that accept wchar_t*
and char16_t* as input.

bResult = FileExists( UnicodeString(pwcPath) );


Or simply let the compiler do an implicit conversion for you:

bResult = FileExists( pwcPath );


bResult = DirectoryExists( // expects UnicodeString );

Same thing.

1. Is there a way to convert from wchar_t to UnicodeString or vice
versa ?

Yes. Use UnicodeString's constructors when converting to UnicodeString,
and use the UnicodeString::c_str() method when converting to wchar_t* or
char16_t*.

2. Why I need to dynamically cast for __fc(
(wchar_t*)Textbox->Text.w_str() ) where .w_str() returns as WideChar*
and WideChar is typedef of wchar_t under help documentation ?

WideChar is a typedef for wchar_t on Windows only. You do not need the cast
in that situation.

On other platforms, WideChar is a typedef for char16_t instead. __fc() only
accepts wchar_t* on all platforms, so you need a cast. But be careful, because
wchar_t is not 16bits on all platforms. On some platforms, wchar_t is 32bits
instead. UnicodeString uses UTF-16 on all platforms, but wchar_t is used
to represent UTF-32 on platforms where wchar_t is 32bits. That is why char16_t
is used instead on those platforms.

Otherwise, change your function to use System::PChar instead so it matches
what c_str() returns on every platform.

3. How come _access() not even implemented in FMX for Android ?

I can't answer that.

4. If I were to use UnicodeString.sprintf(), is there a format specifier
for wchar_t* since there is no %ls and %ls is not supported under
MacOS ?

On OSX, you can use the "%s" format specifier with a UTF-8 string value.
See QC #110383:

[MacOS] UnicodeString.sprintf method does not work on Mac OS X
http://qc.embarcadero.com/wc/qcmain.aspx?d=110383

5. UTF8String().c_str() is it the same as char* in FMX ?

char is just a data type, not an encoding. It can be used to represent any
8bit encoding, including UTF-8.

Technically, the same is true for wchar_t. It can be used to represent any
16bit/32bit encoding, which includes UTF-16/32 on appropriate platforms.

char16_t, on the other hand, was specifically added to C++ to support UTF-16.

6. If statement 5 is TRUE, then what causes it can hold Unicode
character after converted from UTF8String() ?

UTF-7, UTF-8, UTF-16, UTF-32, they are all just binary encodings for the
same underlying character set (Unicode), and thus can be freely converted
between each other without losing any data. A char is 8bits on most platforms
(certainly the platforms that C++Builder supports, anyway), and UTF-8 uses
8bit elements. That is why a char* can represent a UTF-8 string.

7. I often used wchar_t, and when came to CBXE, I have to use
UnicodeString as standard string data handling, but there are time
when legacy written in VC++, I often used wchar_t, therefore, really
need to know is there a way or any resource that can do string
conversion between wchar_t and UnicodeString.

See above.

8. What is the different between char16_t and wchar_t , since both
carried 16 bits per character ?

char16_t is 16bits on every platform (hense its name), but wchar_t is not
16bits on every platform.

--
Remy Lebeau (TeamB)
GAI CHEW KAI

Posts: 117
Registered: 7/25/14
Re: CBXE7 - char16_t, wchar_t, WideChar, UnicodeString. [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 1, 2014 6:41 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Hi Remy, thanks for the great feedback which clear my doubts really :o)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02