TCHAR in VC++

本文详细介绍了使用TCHAR库进行字符串处理的方法,包括字符串复制、连接、比较、长度计算及转换等功能。通过示例展示了如何在Unicode环境中高效操作字符串,适用于初学者掌握TCHAR库的基本用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/* String functions */

_tcscpy_s,

_tcscat_s,

_tcslen, _tcsnlen,

_tcschr,

_tcsdup,

_tprintf, _tprintf_s, _stprintf, _stprintf_s

 /* String conversion functions */

// Convert string to number

_tstoi, _tstof, _tstol, _tcstod, _tcstol, _tcstoul

_ttoi, _ttol, _ttoi64

// Convert number to string

_itot_s, _ltot_s, _ultot_s, _i64tot_s, _ui64tot_s

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 2ndparameter 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);

Noteinet_addr function requires char *.

I hope this post helps beginners. 

https://www.codeproject.com/Articles/192952/String-Manipulations-using-TCHAR-Library

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值