#pragma once #include <iostream> #include <string> using namespace std; //itoa //int ==> string //10进制 string itoa(int nNum) { int nSize = 128; char* pStr = new char[nSize]; memset(pStr,0,nSize); char* pCurr = pStr; char* pBg = pCurr; if(nNum < 0) { *pCurr = ‘-‘; pCurr ++; nNum = 0 - nNum; pBg = pCurr; } while(nNum > 0) { *pCurr = nNum%10 + ‘0‘; pCurr ++; nNum = nNum/10; } pCurr --; //倒置 for(int i=0;i<=(pCurr - pBg)/2;i++) { char cTmp = *(pBg + i); *(pBg + i) = *(pCurr - i); *(pCurr - i) = cTmp; } string str(pStr); delete pStr; return str; } //atoi int atoi(const char* pStr,int nLen = 0) { if(nLen == 0) { nLen = strlen(pStr); } int nValue = 0; int nSub = 1; int nEnd = 0; if(pStr[0] == ‘-‘) { nEnd = 1; } for(int i=nLen-1;i>=nEnd;i--) { nValue += (pStr[i] - ‘0‘)*nSub; nSub *= 10; } if(nEnd == 1) { nValue = 0- nValue; } return nValue; }
原文地址:http://my.oschina.net/u/659405/blog/294446