C++数据类型转换总结

 

1. String

1.1 string CString

string str;

CString cstr(str.c_str());

1.2 string char*

string str;

char* ch;

ch = str.c_str();

1.3string double

string str;

double dou;

dou = atof(str.c_str());

1.4 string int

string str;

int i;

i = std::atoi(str.c_str());

2. CString

2.1 CString char*

CString cstr;

char* ch;

 

//不出现乱码

ch = cstr.GetBuffer(0);

 

// 出现乱码,还需将wchar_t*转为char*

ch = toNarrowString(cstr.GetBuffer(0));

 

2.2 CString string

CString cstr;

string str = CStringA(cstr);

2.3 CString int

CString cstr;

int i;

cstr.Format(”%d”, i);

2.4 Format

CString cstr;

 

// %d转int

cstr.Format(_T(“%d”),temp);

 

// %f转float,double

cstr.Format(_T(“%f”),temp);

 

// %s转string

cstr.Format(_T(“%s”),temp);

3. char*

3.1char* string

char* ch;

string str;

str = ch;

3.2 char* CString

char* ch;

CString cstr(ch);

3.3 char* wchar_t*

//转宽字符的处理

wchar_t* toWideString(const char* pStr)

{

 

    // figure out how many wide characters we are going to get

    setlocale(LC_ALL, ".936");

 

    //获取双字节的个数,不包含结尾的null,所以要 +1

    size_t nChars = mbstowcs(NULL, pStr, 0);

 

    if (nChars == 0)

        return L"" ;

 

    // convert the narrow string to a wide string

    wchar_t* buff;

    buff = new wchar_t[nChars+1];

 

    //转换时将结尾的null-terminate已转换过来

    mbstowcs(buff, pStr, nChars + 1) ;

 

    //为确保正确,补齐null

    buff[nChars] = 0;

 

    setlocale(LC_ALL, "C");

    return buff;

}

 

std::wstring toWideString(const std::string& str)

{

    if (str.empty())

    {

        return L"";

    }

 

    wchar_t* wcRet = toWideString(str.c_str());

    wstring ret(wcRet);

    delete[] wcRet;

    return ret;

}

4. wchar_t*

4.1 wchar_t* char*

//转窄字符的处理

char* toNarrowString(const wchar_t* pStr)

{

    setlocale(LC_ALL, ".936");

 

    //得到需要的字符长度,包括null-terminate

    int nChars = wcstombs(NULL , pStr , 0) ;

 

    if (nChars == 0)

        return "" ;

 

    // convert the wide string to a narrow string

    char* buff = new char[nChars + 1];

 

    //len表示要提取的字符长度,也许小于

    wcstombs(buff , pStr , nChars + 1) ;

    //最后一位 设置为null

    buff[nChars] = 0;

 

    setlocale(LC_ALL, "C");

    return buff;

}

 

std::string toNarrowString(const std::wstring& str)

{

    if (str.empty())

    {

        return "";

    }

 

    char* cRet = toNarrowString(str.c_str());

    string ret(cRet);

    delete[] cRet;

    return ret ;

}

5. int

5.1  int string

//itoa(int, char , radix);

// radix为进制

char ch[10];

int num = 100;

string str;

str = itoa(num, ch , 10);

5.2  int string

6. long

6.1 long double

long lon;

double dou;

dou = (double)lon;

7. double

7.1 double long

double dou;

long lon;

lon = (long)dou;

7.2 double string

       Double dou;

C 货币
2.5.ToString("C")
2.50
D 十进制数

25.ToString("D5")
00025
E
科学型
25000.ToString("E")
2.500000E+005
F
固定点
25.ToString("F2")
25.00
G
常规
……

8. float

 

9. UTF-8

9.1UTF-8 Unicode

wstring String_utf8ToUnicode(const char* src, std::wstring &t)

  {

      if (src == NULL)

      {

          return L"";

      }

 

      int size_s = strlen(src);

      int size_d = size_s + 10;          //?

 

      wchar_t *des = new wchar_t[size_d];

      memset(des, 0, size_d * sizeof(wchar_t));

 

      int s = 0, d = 0;

      //bool toomuchbyte = true; //set true to skip error prefix.

 

      while (s < size_s && d < size_d)

      {

          unsigned char c = src[s];

          if ((c & 0x80) == 0)

          {

              des[d++] += src[s++];

          }

          else if((c & 0xE0) == 0xC0)  ///< 110x-xxxx 10xx-xxxx

          {

              WCHAR &wideChar = des[d++];

              wideChar  = (src[s + 0] & 0x3F) << 6;

              wideChar |= (src[s + 1] & 0x3F);

 

              s += 2;

          }

          else if((c & 0xF0) == 0xE0)  ///< 1110-xxxx 10xx-xxxx 10xx-xxxx

          {

              WCHAR &wideChar = des[d++];

 

              wideChar  = (src[s + 0] & 0x1F) << 12;

              wideChar |= (src[s + 1] & 0x3F) << 6;

              wideChar |= (src[s + 2] & 0x3F);

 

              s += 3;

          }

          else if((c & 0xF8) == 0xF0)  ///< 1111-0xxx 10xx-xxxx 10xx-xxxx 10xx-xxxx

          {

              WCHAR &wideChar = des[d++];

 

              wideChar  = (src[s + 0] & 0x0F) << 18;

              wideChar  = (src[s + 1] & 0x3F) << 12;

              wideChar |= (src[s + 2] & 0x3F) << 6;

              wideChar |= (src[s + 3] & 0x3F);

 

              s += 4;

          }

          else

          {

              WCHAR &wideChar = des[d++]; ///< 1111-10xx 10xx-xxxx 10xx-xxxx 10xx-xxxx 10xx-xxxx

 

              wideChar  = (src[s + 0] & 0x07) << 24;

              wideChar  = (src[s + 1] & 0x3F) << 18;

              wideChar  = (src[s + 2] & 0x3F) << 12;

              wideChar |= (src[s + 3] & 0x3F) << 6;

              wideChar |= (src[s + 4] & 0x3F);

 

              s += 5;

          }

      }

 

      t = des;

      delete[] des;

      des = NULL;

 

      return t;

  }

9.2UTF-8 ansi

string  String_utf8ToAnsi(const char *strutf)

  {

      wstring  wStrTmp;

      String_utf8ToUnicode( strutf, wStrTmp);

      return String_unicodeToAnsi(wStrTmp);

  }

10. Unicode

10.1 Unicode UTF-8

int String_unicodeToUtf8( const wstring& strRes, char *utf8, int nMaxSize )

  {

      if (utf8 == NULL) {

          return -1;

      }

      int len = 0;

      int size_d = nMaxSize;

 

 

      for (wstring::const_iterator it = strRes.begin(); it != strRes.end(); ++it)

      {

          wchar_t wchar = *it;

          if (wchar < 0x80)

          {  //

              //length = 1;

              utf8[len++] = (char)wchar;

          }

          else if(wchar < 0x800)

          {

              //length = 2;

 

              if (len + 1 >= size_d)

                  return -1;

 

              utf8[len++] = 0xc0 | ( wchar >> 6 );

              utf8[len++] = 0x80 | ( wchar & 0x3f );

          }

          else if(wchar < 0x10000 )

          {

              //length = 3;

              if (len + 2 >= size_d)

                  return -1;

 

              utf8[len++] = 0xe0 | ( wchar >> 12 );

              utf8[len++] = 0x80 | ( (wchar >> 6) & 0x3f );

              utf8[len++] = 0x80 | ( wchar & 0x3f );

          }

          else if( wchar < 0x200000 )

          {

              //length = 4;

              if (len + 3 >= size_d)

                  return -1;

 

              utf8[len++] = 0xf0 | ( (int)wchar >> 18 );

              utf8[len++] = 0x80 | ( (wchar >> 12) & 0x3f );

              utf8[len++] = 0x80 | ( (wchar >> 6) & 0x3f );

              utf8[len++] = 0x80 | ( wchar & 0x3f );

          }

 

      }

 

 

      return len;

  }

11. ansi

11.1ansi UTF-8

char *String_ansiToUtf8(const  string&  strSrc)

  {

      wstring  wstrUni = String_ansiToUnicode(strSrc);

      int len = wstrUni.length() * 3 + 1;

 

      char*  chUTF8 = new char[len];

      memset(chUTF8,0x00, len);

      int r = String_unicodeToUtf8(wstrUni,chUTF8, wstrUni.length() * 3);

      if (r < 0){

        puts("toUtf8 error");

      }

      //strRes = chUTF8;

      //delete  []chUTF8;

      return chUTF8;

  }

12. 半角

12.1 半角→全角

  string String_halfToFull(string str)

  {

    //**全角空格为12288,半角空格为32

    //    **其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248

 

    wstring ws = String_ansiToUnicode(str);

    int len =ws.length();

    for(int i=0; i<len; i++)

    {

      if(ws[i]==32)

        ws[i] =ws[i] +12256;

 

      if( 126>=ws[i] && ws[i]>=33)

        ws[i] =ws[i]+65248;

    }

    return  String_unicodeToAnsi(ws);

  }

13. 全角

12.1 半角→全角

14. Format prinf

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值