/////////////////////////////////////////////////////////////////////// // HexConvert.cpp - convert binary data to ASCII hexadecimal // // // // Jim Fawcett, CSE691 - SW Modeling & Analysis, Fall 2000 // /////////////////////////////////////////////////////////////////////// #include #include #include #include using namespace std; #define HalfByteToHex(n) ((char) (((n) > 9) ? ((n) - 10 + 'A') : ((n) + '0'))) //----< convert instance t, of type T, to hex >------------------------ string toHex(unsigned short value) { string temp; const int mask = 0xf; while(value > 0) { temp.insert(0,HalfByteToHex(mask & value)); value = value >> 4; } while(temp.size() < 2*sizeof(unsigned short)) temp.insert(0,'0'); return temp; } //----< test stub >---------------------------------------------------- void main() { int j,k; for(j=0; j<511; j+=8) { cout << "\n "; for(k=0; k<8; k++) { cout << toHex(j+k).c_str() << " "; } } cout << "\n\n"; }