以前使用vc6的时候,messagebox("vc6")可以执行,但是用2005时候必须是这样messagebox(_T("2005")),为什么?
这个问题与使用vc6还是2005无关,跟是否使用unicode有关。即在创建工程时,是否选择了使用unicode.
_T宏指定字符串中的一个字符是一个字节还是两个字节。即使你选择了unicode,系统也不会自动为你存为两个字节。你也要指定。
如果你定义了UNICODE,则messagebox函数会把两个字节当成一个字符来输出。而不管你是否使用了_T.
1 如何将一串字符当成是十六进制数?
这个问题很有意思啊。一个CString类型的数要转换成16进制的数,但是除了类型发生改变之外,所见的数是一样的。比如:一个CString类型的数CString a(“53 4d”); 转换成16进制之后仍然是0x53,4d.两者的不同之处在于:a是一个字符串,0x55和0x66只不过是其内容。a可以等价为:35 33 20 34 64。。。其中35是5的asc码,33是3的,20是空格。。。。这个转换怎么做呢?
答案:
这个函数是可以,不过不大好用.
包含文件和库
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
StrToIntEx 每次最多只能转换一个最大32位的十六进制数
用法比如:
CString x = TEXT("0xAABBCCDD");
int o = 0;
::StrToIntEx(x.GetString(), STIF_SUPPORT_HEX, o);
这样o = 0xAABBCCDD
当你的字符串中没有0x时,你需要自己加上。比如:
temp1 = temp.Mid(j , 2);
temp1 = "0x" + temp1;
StrToIntEx(temp1, STIF_SUPPORT_HEX, &result);
2
把一个字符串当成一个数?
atoi
功 能: 把字符串或cstring转换成整型数.
名字来源:array to integer 的缩写.
函数说明: atoi()会扫描参数nptr字符串,如果第一个字符不是数字也不是正负号返回零,否则开始做类型转换,之后检测到非数字或结束符 /0 时停止转换,返回整型数。
原型: int atoi(const char *nptr);
需要用到的头文件: #include < stdlib.h >
程序例:
1)
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int n;
char *str = "12345.67";
n = atoi(str);
printf("string = %s integer = %d/n", str, n);
return 0;
}
执行结果
string = 12345.67 integer = 12345
2)
#include <stdlib.h>
#include <stdio.h>
int main()
{
char a[] = "-100" ;
char b[] = "123" ;
int c ;
c = atoi( a ) + atoi( b ) ;
printf("c = %d/n", c) ;
return 0;
}
执行结果
c = 23
Convert a string to double (atof and _wtof ), integer (atoi , _atoi64 , _wtoi and _wtoi64 ), or long integer (atol and _wtol ).
double
atof(
const
char
*
string
);
double _wtof(
const
wchar_t
*
string
);
int
atoi
(
const
char
*
string
);
__int64 _atoi64(
const char
*
string
);
int
_wtoi(
const
wchar_t
*
string
);
__int64
_wtoi64(
const wchar_t
*
string
);
long
atol(
const
char
*
string
);
long
_wtol(
const
wchar_t
*
string
);
3
char 数组与 cstring之间的转换
char 转换成 cstring: cstring.format("%s", char)
cstring 转换成 char: 一是强制类型转换LPCSTR。一个是
1. char* 转成CString
(1)可以直接构造函数.如下:
char * p = "test";
CString str(p);
(2)可以用成员函数Format 如下:
char* p = "test;
CString str;
str.Format("%s", p);
2. CString 转 char*
(1)用成员函数GetBuffer() 如下;
CStirng str("test");
char* p = str.GetBuffer(10);
(2)当然也可以用C函数,就不细述了
5
整数与cstring之间的转换:
int a = 0x100;
CString tmp;
tmp.Format("%d",a);// ***%d的意义不是告诉format函数a是个10进制的数(format不需要知道a是个什么样的数,a在内存中以二进制的形式存放),
%d只是告诉format这串二进制数我们把它当成一个整数读出来,再把该整数用字符串显示出来*****
MessageBox(tmp);