How can I get the hex value of a string?

For example, if dat == "dl2beu", then how could I put "646C32626575" in buffer?
If it helps, dat is a string that will always be 6 chars, and buffer is also a string.
Hex value? You mean hash value?
No, I mean a series of the hex values for each ascii character.
0x64 = 'd'
0x6C = 'l'
0x32 = '2'
etc.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#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
Thanks! Worked perfectly in the full code.
Or the same thing using c-strings
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#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;
}
Topic archived. No new replies allowed.