Delphi to C# translation

  • Thread starter Thread starter J
  • Start date Start date
J

J

I would like to translate the following to C#, and am wondering what is the
simpliest way to replace the "( if (ASrc in UnsafeChars) or (ASrc >=
#$80) or (ASrc[1] < #32)" portion of the code.

function urlEncode(const ASrc: string): string;
const
UnsafeChars = ['*', '#', '%', '<', '>', '+', ' '];
var
i: Integer;
begin
Result := '';
for i := 1 to Length(ASrc) do begin
if (ASrc in UnsafeChars) or (ASrc >= #$80) or (ASrc[1] < #32)
then
Result := Result + '%' + IntToHex(Ord(ASrc), 2)
else
Result := Result + ASrc;
end;
end
 
I would like to translate the following to C#, and am wondering
what is the simpliest way to replace the "( if (ASrc in
UnsafeChars) or (ASrc >= #$80) or (ASrc[1] < #32)" portion of
the code.

function urlEncode(const ASrc: string): string;
const
UnsafeChars = ['*', '#', '%', '<', '>', '+', ' '];
var
i: Integer;
begin
Result := '';
for i := 1 to Length(ASrc) do begin
if (ASrc in UnsafeChars) or (ASrc >= #$80) or
(ASrc[1] < #32)
then
Result := Result + '%' + IntToHex(Ord(ASrc), 2)
else
Result := Result + ASrc;
end;
end


J,

The quickest way would be to use the HttpUtility.UrlEncode method
instead of porting the Delphi code.

Hope this helps.

Chris.
 
OK, that helps for the specific case mentioned.

The example supplied is a technique that I commonly use to handle strings,
so need an approach that is more generic. If HttpUtility.UrlEncode did not
exist, what is the simpliest way to replace the "( if (ASrc in
UnsafeChars) or (ASrc >= #$80) or (ASrc[1] < #32)" portion of the code.



Chris R. Timmons said:
I would like to translate the following to C#, and am wondering
what is the simpliest way to replace the "( if (ASrc in
UnsafeChars) or (ASrc >= #$80) or (ASrc[1] < #32)" portion of
the code.

function urlEncode(const ASrc: string): string;
const
UnsafeChars = ['*', '#', '%', '<', '>', '+', ' '];
var
i: Integer;
begin
Result := '';
for i := 1 to Length(ASrc) do begin
if (ASrc in UnsafeChars) or (ASrc >= #$80) or
(ASrc[1] < #32)
then
Result := Result + '%' + IntToHex(Ord(ASrc), 2)
else
Result := Result + ASrc;
end;
end


J,

The quickest way would be to use the HttpUtility.UrlEncode method
instead of porting the Delphi code.

Hope this helps.

Chris.
 
J said:
OK, that helps for the specific case mentioned.

The example supplied is a technique that I commonly use to handle strings,
so need an approach that is more generic. If HttpUtility.UrlEncode did not
exist, what is the simpliest way to replace the "( if (ASrc in
UnsafeChars) or (ASrc >= #$80) or (ASrc[1] < #32)" portion of the code.


You could have UnsafeChars as a string and use IndexOf to test whether
or not it's present - and the other tests are just:

if (ASrc < 32 || ASrc > 0x80)
....

(I'm assuming that #$80 = 128 here, in other words #$ means "hex".)
 
I would like to translate the following to C#, and am wondering what is the
simpliest way to replace the "( if (ASrc in UnsafeChars) or (ASrc >=
#$80) or (ASrc[1] < #32)" portion of the code.

function urlEncode(const ASrc: string): string;
const
UnsafeChars = ['*', '#', '%', '<', '>', '+', ' '];


You could check if your string in C# contains any of the "unsafe"
characters like so:

string sMyStr;

if(sMyStr.IndexOfAny(new char[] {'*', '#', '%', '<', '>', '+', ' }) >=
0)
{
// contains one or many unsafe chars
}

That should do the trick

Marc
 
You could try something like:

public static string UrlEncode(string src)

{

const string unsafeChars = "*#%<>+ ";

string result = "";

for(int i = 0; i < src.Length; i++)

{

char c = src;

if (unsafeChars.IndexOf(c) > 0 || (int)c >= 0x80 || (int)c < 32)

result += "%" + ((int)c).ToString("X");

else

result += c;

}

return result;

}



J said:
OK, that helps for the specific case mentioned.

The example supplied is a technique that I commonly use to handle strings,
so need an approach that is more generic. If HttpUtility.UrlEncode did not
exist, what is the simpliest way to replace the "( if (ASrc in
UnsafeChars) or (ASrc >= #$80) or (ASrc[1] < #32)" portion of the code.



Chris R. Timmons said:
I would like to translate the following to C#, and am wondering
what is the simpliest way to replace the "( if (ASrc in
UnsafeChars) or (ASrc >= #$80) or (ASrc[1] < #32)" portion of
the code.

function urlEncode(const ASrc: string): string;
const
UnsafeChars = ['*', '#', '%', '<', '>', '+', ' '];
var
i: Integer;
begin
Result := '';
for i := 1 to Length(ASrc) do begin
if (ASrc in UnsafeChars) or (ASrc >= #$80) or
(ASrc[1] < #32)
then
Result := Result + '%' + IntToHex(Ord(ASrc), 2)
else
Result := Result + ASrc;
end;
end


J,

The quickest way would be to use the HttpUtility.UrlEncode method
instead of porting the Delphi code.

Hope this helps.

Chris.
 
OK, that helps for the specific case mentioned.

The example supplied is a technique that I commonly use to
handle strings, so need an approach that is more generic. If
HttpUtility.UrlEncode did not exist, what is the simpliest way
to replace the "( if (ASrc in UnsafeChars) or (ASrc >=
#$80) or (ASrc[1] < #32)" portion of the code.


J,

In addition to the other answers posted here, a "set" data type like
Delphi's would do the trick. Since C# doesn't have that, a gentleman
named Jason Smith kindly wrote one:

http://www.codeproject.com/csharp/Sets.asp

Chris.
 
Back
Top