Converting a string variable to a string of hex

Hi Im trying to encode a string to a hex string to later use as a URL to navigate a web page, but first I will need to convert my string variable to a string of hex saved in a variable.
Can some one please help me achieve this?

int main(void)
{
string email = "test@gmail.com";
string pass = "password";
string ip = "10.10.10.10";
string login ="username=" + email + "&password=" + pass + "&ip=" + ip;
string res;
int loc = 0;

while (login[loc] != NULL)
{
int temp = hex(login[loc]);
res += temp;
loc++;
}
string url = "http://" + res+ ".com"
return 0;
}
now this code dose not work it is an example of what I am trying to achieve,
my apologizes for being new to C++, I only have experience with AutoIt and can achieve this code as follows :-

while $loc <= StringLen($loginid)
$RES = $RES & hex(Asc(StringMid($loginid,$loc,1)),2)
$loc += 1
WEnd

I would very much like to know how to do this in C++
To convert a char to hex:

1
2
3
4
char ch = 'X'; 

static const char* hex = "0123456789abcdef";
std::cout << hex[ ch >> 4 ] << hex[ ch & 0x0f ] << std::endl;

thanks for the reply,
how can i convert this string and save as a variable, not print it in a window.

I will use it in some code to navigate a web browser to the desired page and will not ever print this string,

is there a way to just convert it and save as a variable? ive found quite a few examples on how to print as a hex string but havent found any examples not using the cout object and using the hex flag to print to a hex string,

cout << hex << int(login[loc])<< endl;
prints the char as a hex but I am trying to just convert it and save as a variable to use later in the code,
Last edited on
is there any 1 that can help me with my problem ?
@jsmith

sorry for not understanding what you wrote earlier, that code works well!

I do not understand how it works and that is the important part but for now i am able to achieve my goal!

thank you!

Problem Solved.
Last edited on
Topic archived. No new replies allowed.