先看MSDN上对_tcstol的解释
1、在不同平台上的不同形式TCHAR.H Routine | _UNICODE & _MBCS Not Defined | _MBCS Defined | _UNICODE Defined |
_tcstol | strtol | strtol | wcstol |
long strtol( const char *nptr, char **endptr, intbase);
long wcstol( const wchar_t *nptr, wchar_t **endptr, intbase);
其中:[in] nptr 表示要进行扫描字符串指针
[out] endptr 存储扫描后无法转化的剩余的字符串
[base] 表示数字的基,取值为:2,8,10,16 对应的进制你懂的
这里我引用PHP中字符串匹配法则中的一个:贪婪匹配!!!也就是说,该函数会最大程序地匹配所有满足当前进制的字符,将其转化为对应的数字!!
例子:
- (1).转二进制
- #include"afx.h"
- #include<stdio.h>
- void main()
- {
- char c[5]="0131";
- CString cs=_T(c);
- LPTSTR pstr = NULL;
- int last=_tcstol(c,pstr ,2);//2表示二进制
- printf("%d\n",last);//使用十进制输出结果为1
- printf("%s\n",pstr);//输出:31
- }
再来一个例子吧
- 2).转八进制
- #include"afx.h"
- #include<stdio.h>
- void main()
- {
- char c[7]="010a,!";
- CString cs=_T(c);
- LPTSTR pstr = NULL;
- int last=_tcstol(c,pstr ,8);//8表示8进制printf("%d\n",last);//使用十进制输出结果为8
- printf("%s\n",pstr);//输出:a,!
- }
参考文章:http://fpcfjf.blog.163.com/blog/static/55469793201015111136406/