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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert C++ function to C#

Status
Not open for further replies.

developer155

Programmer
Joined
Jan 21, 2004
Messages
512
Location
US
This for whoever knows a few things about C++:

Hello I have this function to convert to C#. I never worked with C++ so some operators seem confusing. Can someone help me with conversion to C#. Among other things,
how is this possible:
digits[digitIndex] + 'A' - 10;
digits is a int array, how can we add a char and subtract an int from it????

string encodeURLfield(string URLfield)
{
string encodedField = "";
const char *URLfieldC_str = URLfield.c_str();
int URLfieldLength = URLfield.length();
int URLfieldIndex;
int digits[2];
int digitIndex;
char currentChar;
char digitChars[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';

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

encodedField = encodedField + "%" + digitChars;
}
}

return(encodedField);
}


thanks for any help
 
In C or C++, a char is just a subset of int. An array of two ints is essentially the same thing as an array of two char. What's acutally stored in a char array is the ASCII value of each character. 'A' = 65, 'B' = 66, etc. Reading chars out of an int array is a shorthand way to translate the integers values to ASCII values.

Code:
                digitChars[digitIndex] = digits[digitIndex] < 10 ?
                    digits[digitIndex] + '0' :
                    digits[digitIndex] + 'A' - 10;
That snippet appears to be converting 0-9 to '0'-'9' and translating 10 and up into uppercase letters.

Jeff
[purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day

I was not born cynical - I earned my cynicism through careful observation of the world around me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top