10进制转16进制,字符串格式。 代码: string& DecToHex( string &hex, const string &dec ) { stringstream tmp ; tmp<< dec ; int value = 0 ; tmp>> value ; tmp.clear() ; tmp.setf( ios::hex, ios::basefield ) ; tmp<< value ; tmp>> hex ; return hex ; } 简单验证: int main() { string dec( "255" ) ; string hex ; hex = DecToHex( hex, dec ) ; cout<<"dec=" <<dec <<endl ; cout<<"hex=" <<hex <<endl ; cout << "Hello world!" << endl; return 0; }