游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2075|回复: 4

请问有什么简单的方法将INT转成STRING吗?

[复制链接]

9

主题

36

帖子

36

积分

注册会员

Rank: 2

积分
36
发表于 2005-2-26 12:22:00 | 显示全部楼层 |阅读模式
还有将STRING转成INT
发觉CString挺方便,不过非MFC程序就用不了了

20

主题

398

帖子

398

积分

中级会员

Rank: 3Rank: 3

积分
398
发表于 2005-2-26 16:55:00 | 显示全部楼层

Re:请问有什么简单的方法将INT转成STRING吗?

我没找到什么简单的方法,自己编个转换函数吧!

我编过一个,你看看:
CString Int_CString( int Number )
{
        int INumber=Number, i=0, p=0, n=0;
        CString CNumber;
        BOOL Negative=FALSE;

        if(INumber==0)
        {
                CNumber="0";
                return CNumber;
        }
       
        if(INumber<0)
        {
                INumber=-INumber;
                Negative=TRUE;
        }

        while(INumber-Involution( 10, i )>=0)
        { i++; }

                n=i;
        for( i=n-1; i>=0; i--)
        {
                while(INumber>=Involution(10,i))
                {
                        INumber=INumber-Involution(10,i);
                        p++;
                }
                CNumber=CNumber+Int_CString_A(p);
                p=0;
        }

        if(Negative)
                CNumber="-"+CNumber;

        return CNumber;
}

CString Int_CString_A( int i )
{
        CString CNumber;

        switch(i)
        {
        case 1:
                CNumber="1";
                break;
        case 2:
                CNumber="2";
                break;
        case 3:
                CNumber="3";
                break;
        case 4:
                CNumber="4";
                break;
        case 5:
                CNumber="5";
                break;
        case 6:
                CNumber="6";
                break;
        case 7:
                CNumber="7";
                break;
        case 8:
                CNumber="8";
                break;
        case 9:
                CNumber="9";
                break;
        case 0:
                CNumber="0";
                break;
        }

        return CNumber;
}

int Involution( int Number, int inv )
{
        int INumber=Number, i;

        if(inv==0)
        { INumber=1; }

                else if(inv>0)
        {
                for(i=1; i<inv; i++)
                { INumber=INumber*Number; }
        }

        return INumber;
}

long CString_Int(CString str)
{
        long Int=0;
        int len=str.GetLength();
        int chrnum=0, start=0;
        CString chr;
        BOOL Negative=FALSE;

        chr=str.GetAt(start);
        if(chr=="-")
        {
                Negative=TRUE;
                start++;
        }
               
        for(chrnum=start; chrnum<len; chrnum++)
        {
                chr=str.GetAt(chrnum);
                if(CString_Int_A(chr)!=0)
                {
                    if(chrnum!=len-1)
                      Int+=Involution(10, len-chrnum-1) * CString_Int_A(chr);
                    else
                      Int+=CString_Int_A(chr);
                }
        }

        if(Negative)
                Int=-Int;

        return Int;
}

int CString_Int_A(CString str)
{
        int _int;

        if(str=="0")
                _int=0;
        if(str=="1")
                _int=1;
        if(str=="2")
                _int=2;
        if(str=="3")
                _int=3;
        if(str=="4")
                _int=4;
        if(str=="5")
                _int=5;
        if(str=="6")
                _int=6;
        if(str=="7")
                _int=7;
        if(str=="8")
                _int=8;
        if(str=="9")
                _int=9;

        return _int;
}

9

主题

36

帖子

36

积分

注册会员

Rank: 2

积分
36
 楼主| 发表于 2005-2-26 17:21:00 | 显示全部楼层

Re:请问有什么简单的方法将INT转成STRING吗?

呵呵,回楼上,如果能用CString的话也不用这么麻烦了,它直接提供转换的方法,现在的问题是不是MFC项目就用不了CString了

8

主题

111

帖子

163

积分

注册会员

Rank: 2

积分
163
发表于 2005-2-26 18:22:00 | 显示全部楼层

Re:请问有什么简单的方法将INT转成STRING吗?

atoi(...)和itoa(...)
用来做字符串和数字间的转换的.

20

主题

398

帖子

398

积分

中级会员

Rank: 3Rank: 3

积分
398
发表于 2005-2-27 11:59:00 | 显示全部楼层

Re:请问有什么简单的方法将INT转成STRING吗?

      我没试过非MFC使用CString,不过CString只是一个处理字符串的类,我想在非MFC的程序中应该也能使用吧!不知你遇到了什么问题?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-12-24 12:51

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表