Re: Using TCHAR with pcap and sockets (send/recv/setsockopt)
Rayne wrote:
I'm really confused by this unicode vs multi-byte thing.
Say I'm compiling my program in Unicode (but ultimately, I want a
solution that is independent of the character set used).
1) Will all 'char' be interpreted as wide characters?
No. The datatypes '[[un]signed] char' and 'wchar_t' never change their
actual type. What does change is what 'TCHAR' etc resolves to and
accordingly all APIs using it.
2) If I have a simple printf statement, i.e. printf("Hello World\n");
with no character strings, can I just leave it be without using
_tprintf and _T("...")?
Yes.
If the printf statement includes a character string, then I should use
_tprintf and _T("..."), i.e. _tprintf("Hello %s\n", name); ?
No, not necessarily. Take a look at '%hs' and '%ls' placeholders. Note that
printf() has limited functionality, using wprintf() would be a more
versatile alternative. However, then you can also drop the whole
TCHAR-thing completely, which IMHO is not just an alternative but should be
the goal, unless you have to support win9x.
3) If I have a text file (saved in the default format, i.e. without
changing the default character set used) that I want to read into a
buffer, can I still use char instead of TCHAR?
Yes. However: You could read the bytes in a text file, but without knowing
the encoding you can not interpret it. The 'default character set' you
mention is not universally fixed, but depends on the OS setup.
Actually, using TCHAR for files is a Damn Bad Idea(tm). The problem, just
like with network protocols, is that you don't know the actual encoding. It
might be the configured multibyte encoding or it could end up as
little-endian UTF-16, depending on the program it was written with. Without
knowing the encoding, you can not reliably parse such a file. You as a
programmer should decide the encoding as part of your design. Even if you
just say ASCII (which excludes any chars >= 127) that is fine, too. If you
need further Unicode features, I'd suggest you switch to UTF-8, which is
the the most common variant.
Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite
Sator Laser GmbH
Gesch??ftsf??hrer: Thorsten F??cking, Amtsgericht Hamburg HR B62 932