Click here to Skip to main content
Click here to Skip to main content

String Manipulations using TCHAR Library

By , 15 Jan 2013
 

Introduction

To write Win32 application or MFC application, it may happen that you want to add Unicode support. Unicode gives you the freedom to include texts from any language in your application. On the contrary despite the advantages of Unicode you may decide that you will never use Unicode in your program. In both cases, TCHAR library will help you a lot. 

Tips and Tricks 

If you want to use Unicode in project properties, select "Use Unicode Character Set" as character set. From now on, you can easily use Unicode strings in your code wrapping with a macro. The same macro works fine even if you selected not to use "Unicode Character Set". Code remains same while you have the advantage. 

To use a Unicode string as a function parameter, wrap it with macro _T or TEXT macro. For example:

AfxMessageBox(_T("You clicked it!"));  

_T() and TEXT() are macros from tchar header file. TCHAR library automatically maps functions to Unicode when Unicode is defined. Using TCHAR library helps us to move code to multibyte stream (or unicode) whenever required. Try to avoid primitive data type char array or char *. This is because before using them in your controls, you have to convert them. Repetitive conversion may be tedious. 

Use TCHAR instead of char and use TCHAR* instead of char*. TCHAR* can be written LPTSTR. For const TCHAR*, you may write LPCTSTR which is required when a string is passed as an argument to a function where modification should be restricted.

To calculate length of strings, use _tcslen function:

len = _tcslen(str); 

To compare strings instead of strcmp and strncmp, use _tcscmp and _tcsncmp. Here’s an example:

if (!_tcsncmp(line, _T("desiredtext"), 11))
    AfxMessageBox(_T(“Got desired text.”));

For copying string _tcscpy_s and _tcsncpy_s instead of strcpy and strncpy:

_tcsncpy_s(dest, srcstr, 20);

Note, strpy or _tcscpy_s, don’t put a null after copying the string so remember to set null after copying the string when required.

For string concatenation, use _tcscat_s and _tcsncat_s instead of strcat and strncat. Here, the 2nd parameter is the size of the destination string.

_tcsncat_s(timestamp, 20, str, i);

For splitting tokens, use _tcstok_s instead of strtok.

LPTSTR  next_token;
token = _tcstok_s(str, delim, &next_token);

To convert a string to integer, you can use _ttoi() function.

CString str = _T("10");
CString temp;
temp.Format(_T(" length: %d"), _ttoi(str));
AfxMessageBox(str+temp);

When I didn't know about this handy function I wrote the following function to do the same task. Having a look at the function may help you as an example code: 

int GetEquivValue(TCHAR ch, int base) {
    int diff = _T('a') - _T('A');

    // Invalid base
    if (base >= 16)
        return 0;

    if (base == 16) {
        // make upper case if lowercase
        if (ch >= _T('a') && ch <= _T('z'))
            ch -= diff;

        diff = _T('A') - _T('0');

        if (ch >= _T('A') && ch <= _T('F'))
            return (ch-diff);
    }

    if (ch >= _T('0') && ch <= _T('9'))
        return (ch - _T('0'));
    return 0;
}

int SAatoib(LPTSTR  str, int base) {
    int res = 0;
    int i, len, tmp;

    len = _tcslen(str);
    for (i=0; i<len; i++) {
        tmp = GetEquivValue(str[i], base);
        res = res*base + tmp;
    }
    return res;
} 

Using these functions to convert a string to decimal number, you have to write like this: 

int decVal = SAatoib((LPTSTR )NumStr, 10);  

To convert a string to hexadecimal number, you have to write like this: 

int decVal = SAatoib((LPTSTR )NumStr, 16);  

To convert a string to octal number, you have to write: 

int decVal = SAatoib((LPTSTR )NumStr, 8);  

To format the string like printf, you can use CString::Format function. For example, to get IP address: 

unsigned long int ipsegval[4] = {1, 2, 3, 4};
CString ip;
ip.Format(_T("%u.%u.%u.%u"), ipsegval[0], ipsegval[1], ipsegval[2], ipsegval[3]);

For example, you have a CString. But you need it as const char* or char* The cast to LPCTSTR from CString is a valid one, but cast to const char* isn’t valid. But there is a way. Here’s an example:

CString ipaddrstr(_T(“1.2.3.4”));
CStringA ipaddrstrA(ipaddrstr);
ipaddr = inet_addr(ipaddrstrA);

Note: inet_addr function requires char *.

I hope this post helps beginners. 

History 

  • 16th January, 2012: Version 4 
  • 11th May, 2011: Version 3
  • 9th May, 2011: Version 2
  • 6th May, 2011: Initial version

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

About the Author

Saint Atique
Software Developer Reve Systems
Bangladesh Bangladesh
Member
Interested in programming and english literature.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberCelPlusPlus15 Jan '13 - 0:02 
A fine summary of the most important operations for TCHAR* strings, well written. Thank you.
GeneralMy vote of 2memberMartin Richter [MVP C++]17 May '11 - 20:43 
See my posting.
GeneralTCHAR doesn't define Unicode in all cases [modified]memberMartin Richter [MVP C++]17 May '11 - 20:35 
As Selvin already wrote:
The meaning of TCHAR depends on the project settings and it doesn't imply that Unicode string functions are used.
The articles title is miss leading.
 
I miss the description of the "main idea" behind using TCHAR:
- Write a program that can be used with a single byte character set and a double byte character set, just depending on a compiler switch -
 
If you want to describe Unicode alone, you should use all those w... functions L"..." and WinAPI ...W functions.
--
Martin Richter (MVP for C++) WWJD http://blog.m-ri.de
"A well-written program is its own heaven; a poorly written program is its own hell!" The Tao of Programming
modified on Wednesday, May 18, 2011 2:43 AM

GeneralRe: TCHAR doesn't define Unicode in all casesmemberSaint Atique18 May '11 - 6:19 
That's right. Better it could be 'string manipulation using TCHAR library' as my examples around TCHAR. Thanks for you comment.
GeneralRe: TCHAR doesn't define Unicode in all cases [modified] [modified]memberSaint Atique15 Jan '13 - 9:08 
Hi Martin Richter,
I have corrected the misleading sentences [waiting for approval]. Please check.
 
Many thanks to you.

-- modified 15 Jan '13 - 15:16.
GeneralMy vote of 5membermaplewang10 May '11 - 14:50 
Thanks, you have a good heart to help us.
GeneralAvoid C-style stringsmembermluri10 May '11 - 1:20 
The first recommendation should be to avoid C-style strings alltogether. Whenever possible, use a proper string class, like CString or the std::string family. Thus, you get rid of many of the problems of using arrays of characters as strings, like how to grow them, having to remember to put NULL at the end, having to remember to check sizes before doing certain things...
GeneralRe: Avoid C-style stringsmemberSaint Atique10 May '11 - 9:04 
I agree with you.
GeneralRe: Avoid C-style stringsmemberPJ Arends15 Jan '13 - 10:04 
Using std::string (or std::wstring) will lead to the same problem that the author was originally trying to avoid, that is UNICODE / ANSI conflicts. I always use std::tstring[^] when using STL strings.
Independent ACN Business Owner

Need a new cell phone? We supply most of the major carriers. Telus in Canada. Flash, Verizon, T-Mobile and Sprint in the USA. O2, talkmobile, tmobile, orange, three, and vodafone in Europe. See my website for details.


Within you lies the power for good - Use it!


GeneralRe: Avoid C-style stringsmemberSaint Atique15 Jan '13 - 18:04 
That's a nice input. Thanks.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 15 Jan 2013
Article Copyright 2011 by Saint Atique
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid