1234567891011121314151617181920
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { string dat = "dl2beu"; ostringstream os; for (int i=0; i<dat.length() ; i++) os << hex << uppercase << (int) dat[i]; string hexdat = os.str(); cout << "Text: " << dat << endl;; cout << "Hex: " << hexdat << endl; return 0; }
Text: dl2beu Hex: 646C32626575
123456789101112131415161718
#include <cstdio> #include <cstring> using namespace std; int main() { char dat[] = "dl2beu"; char hexdat[13]; sprintf(hexdat, "%X%X%X%X%X%X", dat[0], dat[1], dat[2], dat[3], dat[4], dat[5]); printf("Text: %s\n", dat); printf("Hex: %s\n", hexdat); return 0; }