I am currently trying this:
Array of characters and hexadecimal units:
Code: Select all
string letters[95] = {" ","!","\"","#","$","%","&","'","(",")","*","+",",","-",
".","/","0","1","2","3","4","5","6","7","8","9",":",";","<",
"=",">","?","@","A","B","C","D","E","F","G","H","I","J","K","L","M","N",
"O","P","Q","R","S","T","U","V","W","X","Y","Z","[","\\","]","^","_",
"'","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p",
"q","r","s","t","u","v","w","x","y","z","{","|","}","~"};
string hexadecimals[95] = {"20","21","22","23","24","25","26","27","28","29","2A","2B","2C","2D",
"2E","2F","30","31","32","33","34","35","36","37","38","39","3A","3B","3C",
"3D","3E","3F","40","41","42","43","44","45","46","47","48","49","4A","4B","4C","4D","4E",
"4F","50","51","52","53","54","55","56","57","58","59","5A","5B","5C","5D","5E","5F",
"60","61","62","63","64","65","66","67","68","69","6A","6B","6C","6D","6E","6F","70",
"71","72","73","74","75","76","77","78","79","7A","7B","7C","7E","7F"};
Then I try to convert them like this:
Code: Select all
char *charToHex(string st)
{
char *hex = "";
hex = new char[st.length()];
for(unsigned int i=0; i<95; i++)
{
if(st.find(letters[i]) !=string::npos)
{
st = ReplaceAllG(st, letters[i], hexadecimals[i]);
}
}
strcpy(hex, st.c_str());
return hex;
delete[] hex;
}
It all works fine unless there is an integer or other non-letter character such as $, %, #, and ;.
Any idea WHY it does this? It's driving me insane, I've looked over it several times and tried new solutions, to no avail.
Also, instead of using that way, I also tried this:
Code: Select all
for(unsigned int i=0; i<95; i++)
{
pos = st.find(letters[i]);
if(pos != string::npos)
st.replace(pos, 1, hexadecimals[i]);
pos += 2;*/
}
This should be simple, but for some reason the ways I'm doing it aren't working. Any ideas, or a better way to do it? thanks..[/code]