developer155
Programmer
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;
}
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;
}