一个我写的通用的很慢的整数转换为字符串的算法,哈哈
受一个网友提示,不过他的我看不懂,根据他神侃般的解释用模和除算法实现的.其实很通用,还可以再扩展,不过速度一定没法和大师们写的库函数相比 :) 别用在效率要求高的地方啊.
//最大兼容的整数转换//效率不高//2进制std::string uitoa_2(unsigned __int64 v){ std::string r = ""; std::string z = ""; //unsigned 不会有负数的 //if (v<0) //{ // v = 0-v; // z = '-'; //} char c; byte b; unsigned __int64 d = 1;//分母 unsigned __int64 i = 0;//当前数值 while(1) { b = (v % (d*2))/d; c = b + '0'; //if(b>9) c = b + 'A'-10; if (v/d == 0)break; r = c + r; d = d * 2; } return z + r;}////最大兼容的整数转换//效率不高//16进制std::string uitoa_hex(unsigned __int64 v){ std::string r = ""; std::string z = ""; //unsigned 不会有负数的 //if (v<0) //{ // v = 0-v; // z = '-'; //} char c; byte b; unsigned __int64 d = 1;//分母 unsigned __int64 i = 0;//当前数值 while(1) { b = (v % (d*16))/d; c = b + '0'; if(b>9) c = b + 'A'-10; if (v/d == 0)break; r = c + r; d = d * 16; } return z + r;}////最大兼容的整数转换//效率不高std::string uitoa(unsigned __int64 v){ std::string r = ""; std::string z = ""; //unsigned 不会有负数的 //if (v<0) //{ // v = 0-v; // z = '-'; //} char c; byte b; unsigned __int64 d = 1;//分母 unsigned __int64 i = 0;//当前数值 while(1) { b = (v % (d*10))/d; c = b + '0'; if (v/d == 0)break; r = c + r; d = d * 10; } return z + r;}////最大兼容的整数转换//效率不高std::string itoa(signed __int64 v){ std::string r = ""; std::string z = ""; //unsigned 不会有负数的 if (v<0) { v = 0-v; z = '-'; } char c; byte b; unsigned __int64 d = 1;//分母 unsigned __int64 i = 0;//当前数值 while(1) { b = (v % (d*10))/d; c = b + '0'; if (v/d == 0)break; r = c + r; d = d * 10; } return z + r;}//std::string string2hex(std::string v){ std::string r = ""; std::string one = ""; for (size_t i=0;i<v.size();i++) { char c = v[i]; one = uitoa_hex(c); if(one.size()==2) { r += one; } else { r += '0' + one; } } return r;}//