Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

strlen, wcslen and UNICODE

345 views
Skip to first unread message

Gerd

unread,
Jun 29, 2007, 6:02:30 AM6/29/07
to
Hi,

I write some source code that should work in both cases - when
compiled with UNICODE enabled and disabled. So I define the strings
using TCHAR. But when I want to work with the strings (calculate the
length, or concatenate two strings), how can I do it independently? To
determine the length there are "strlen" for ascii-strings and "wcslen"
for UNICODE. Is there a way to write on call for both or would I have
to do it like this:

#ifdef _UNICODE
strlen (...);
#else
wcslen (...);
#endif

Many thanks for help.

Gerd

Ulrich Eckhardt

unread,
Jun 29, 2007, 7:21:32 AM6/29/07
to

1. The easiest way out is to use _tcslen(), which itself is a macro that
resolves to strlen() or wcslen(), depending on _UNICODE.

2. You could save yourself some hassle if you simply used a TCHAR-based
string class like std::basic_string<TCHAR> or MFC's/ATL's CString. This
requires C++ though, I don't know if you're using that.

3. Just don't. The interesting feature of TCHAR is that you can support a
codebase that compiles in two different ways and thus supports win95 and
win98. Now, first thing here: do you really want to support ten-year old
desktop OSs? Secondly, if you really want to, you could look at the
Unicode-libraries for win9x. Those are helpers to get _UNICODE-compiled
programs to run to a sufficient extent on win9x.

In general, ask yourself what kind of string type is the most appropriate?
If it is wchar_t, use this, if it is char, use that. If you want to support
Unicode, either use wchar_t (95%) or some specialised library like ICU
(99%). For mere logging, use char/ASCII.

= just my 2cc from maintaining a TCHAR program for quite some time =

Uli

--
Sator Laser GmbH
Geschäftsführer: Ronald Boers, Amtsgericht Hamburg HR B62 932

Gerd

unread,
Jun 29, 2007, 10:46:22 AM6/29/07
to

Ulrich Eckhardt schrieb:

> 1. The easiest way out is to use _tcslen(), which itself is a macro that
> resolves to strlen() or wcslen(), depending on _UNICODE.

Thanks, I will do it.

> 2. You could save yourself some hassle if you simply used a TCHAR-based
> string class like std::basic_string<TCHAR> or MFC's/ATL's CString. This
> requires C++ though, I don't know if you're using that.

The customer requires a 100% pure C application, so this is not
possible.

> 3. Just don't. The interesting feature of TCHAR is that you can support a
> codebase that compiles in two different ways and thus supports win95 and
> win98. Now, first thing here: do you really want to support ten-year old
> desktop OSs? Secondly, if you really want to, you could look at the
> Unicode-libraries for win9x. Those are helpers to get _UNICODE-compiled
> programs to run to a sufficient extent on win9x.

The app must run W2k and more recent versions (due to hardwaredriver ,
which is not available for older systems)

> In general, ask yourself what kind of string type is the most appropriate?
> If it is wchar_t, use this, if it is char, use that. If you want to support
> Unicode, either use wchar_t (95%) or some specialised library like ICU
> (99%). For mere logging, use char/ASCII.

The app must support greek, swedish, french, german, so UNICODE would
be best.

> = just my 2cc from maintaining a TCHAR program for quite some time =
>
> Uli
>
> --
> Sator Laser GmbH
> Geschäftsführer: Ronald Boers, Amtsgericht Hamburg HR B62 932

Thanks,
Gerd

Klueless

unread,
Jun 30, 2007, 1:18:34 AM6/30/07
to
"Gerd" <Ger...@t-online.de> wrote in message news:1183111350.8...@q69g2000hsb.googlegroups.com...

> determine the length there are "strlen" for ascii-strings and "wcslen"

_tcslen and you can find more such functions here:
<http://msdn2.microsoft.com/en-us/library/tsbaswba(VS.80).aspx>

0 new messages