Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Encode URL function

Status
Not open for further replies.

developer155

Programmer
Joined
Jan 21, 2004
Messages
512
Location
US
Hello I have the following function that is supposed to take a string of input and convert it to QueryString friendly version. I tried to re-write the function from C++.
Expected result: if the input is something like "my input" No special characters) the fun returns "my+input".
If input is something like "my,input" the result should be something like "my%2C +input".
Currenlty it works fine when the input does not contain special characters, but it doe snot work as exprected when special characters exist. Looks like digitChars array is not returning the correct value
Here is is:
string encodeURLfield(string URLfield)
{
string encodedField = "";
char[] URLfieldC_str;
URLfieldC_str=URLfield.ToCharArray();
int URLfieldLength = URLfield.Length;
int URLfieldIndex;
int[] digits;
digits = new int[2];
int digitIndex;
char currentChar;
char[] digitChars;
digitChars = new char[3];


for (URLfieldIndex = 0; URLfieldIndex < URLfieldLength;
++URLfieldIndex)
{
if ((currentChar = (URLfieldC_str[URLfieldIndex])) == ' ')
encodedField += '+';
else if ((currentChar >= 'A' && currentChar <= 'Z') ||
(currentChar >= 'a' && currentChar <= 'z') ||
(currentChar >= '0' && currentChar <= '9'))
encodedField += currentChar;
else
{
digits[0] = currentChar >> 4;
digits[1] = currentChar & 0x0F;
digitChars[2] = '\0';
//int zero=0;

for (digitIndex = 0; digitIndex < 2; ++digitIndex)
{
digitChars[digitIndex] = digits[digitIndex] < 10 ?
Convert.ToChar(digits[digitIndex] + '0') :
Convert.ToChar(digits[digitIndex] + 'A' - 10);

}

encodedField = encodedField + "%" + Convert.printArray(digitChars);
}
}

return(encodedField);
}




public string printArray(char[] arr)
{
string result="";
for (int i = 0 ; i < arr.Length ; i++)
result=result + arr.ToString();

return result;

}
 
Take a look at "escape()" and "unescape()" functions from javascript....

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top