///////////////////////////////////////////////////// // MT4Q1 - Convert to and from Hexadecimal // // // // Jim Fawcett, CSE687 - OOD Midterm, Spring 2013 // ///////////////////////////////////////////////////// #include #include #include #include #include "../MTutility/MTutility.h" std::string ToHex(long int src) { std::ostringstream conv; conv.setf(std::ios::showbase | std::ios::uppercase); conv << std::hex << src; return conv.str(); } long int FromHex(const std::string& src) { std::istringstream in(src); long int dest; in.setf(std::ios::hex, std::ios::basefield); in >> dest; return dest; } int main() { Title("MT4Q1 - To and From Hexadecimal"); std::cout << "\n the hex value of " << 27 << " is "; std::string shex = ToHex(27); std::cout << shex; std::cout << "\n the dec value of " << shex << " is " << FromHex(shex); std::cout << "\n"; for(int i=0; i<32; ++i) { std::string hexStr = ToHex(i); long int j = FromHex(hexStr); std::cout << "\n " << j << " : " << hexStr; } std::cout << "\n\n"; return 0; }