学过VB,现在转到VC了,发现除了MFC系统庞大以外,还有一点,就是转化函数不多。本来认为字符串操作函数也很少,后来看到一个CString类,知道了通过这个类的操作可以实现VB中基本的字符串操作函数,如截取,分割等等。但是我怎么找也找不到一个类似VB中的 CInt,CLng,CByte之类的转化函数,如:
有一个字符串,为 “200”
另外有一个整型变量如 I 我想要把这个200给I,那么在VB中可以简单得用   I = CInt("200")同样也可以转化为CLng等等,甚至于这个字符串也可以带十六进制,八进制的前缀,如:
I = CInt("&H200")   这个值是十进制的512那么在VC中,是否有这样的函数呢?来将一个字符串转化为数字(这个数字可能是整型,浮点,或字节)谢谢啦~~

解决方案 »

  1.   

    int i = atoi(string); // 将string转换为数字整型。还有long、double等,参见MSDN
    CString s;
    s.Format("%d", i); // 将i转换为字符串
      

  2.   

    VC中提供了 C 语言函数库 int   i = atoi("32767");
    long  l = atol("1400000000");
    float f = atof("1.55f");以及将数值转成字符串的函数
    char buff[16];
    itoa(32767,buff,10);       //最后一位为进制位
    ltoa(1400000000,buff,10);
      

  3.   

    前天刚研究过
    1)))CString-〉数字
    用atoi就可以了
    要是long int 用_atoi64 或atol
    至于float,double 用atof 按进制转换可以考虑用 _tcstoul()和tcstol()前者好像返回是无符号的
    _tcstoul(hex, 0, 16 )这是串到16进制转换, 至于具体参数你自己查查吧个你个例子:
    // crt_atof.c
    #include <stdlib.h>
    #include <stdio.h>int main( void )
    {
       char *s; double x; int i; long l;   s = "  -2309.12E-15";    /* Test of atof */
       x = atof( s );
       printf( "atof test: \"%s\"; float:  %e\n", s, x );   s = "7.8912654773d210";  /* Test of atof */
       x = atof( s );
       printf( "atof test: \"%s\"; float:  %e\n", s, x );   s = "  -9885 pigs";      /* Test of atoi */
       i = atoi( s );
       printf( "atoi test: \"%s\"; integer: %d\n", s, i );   s = "98854 dollars";     /* Test of atol */
       l = atol( s );
       printf( "atol test: \"%s\"; long: %ld\n", s, l );
    }2))至于  数字——〉CString 就好办了
    用CString的format函数就可以转化
    例如:
    int p = 55;
    CString str;
    str.Format("%d",p);%d你应该知道吧,就是C C++整形格式输出  
    %u 无符号整形
    等等就不赘述了