473,466 Members | 1,514 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Delphi to C# translation

J
I would like to translate the following to C#, and am wondering what is the
simpliest way to replace the "( if (ASrc[i] in UnsafeChars) or (ASrc[i] >=
#$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[i] in UnsafeChars) or (ASrc[i] >= #$80) or (ASrc[1] < #32)
then
Result := Result + '%' + IntToHex(Ord(ASrc[i]), 2)
else
Result := Result + ASrc[i];
end;
end
Nov 15 '05 #1
6 3064
"J" <se******@sendspam.com> wrote in
news:vo************@corp.supernews.com:
I would like to translate the following to C#, and am wondering
what is the simpliest way to replace the "( if (ASrc[i] in
UnsafeChars) or (ASrc[i] >= #$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[i] in UnsafeChars) or (ASrc[i] >= #$80) or
(ASrc[1] < #32)
then
Result := Result + '%' + IntToHex(Ord(ASrc[i]), 2)
else
Result := Result + ASrc[i];
end;
end


J,

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

Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 15 '05 #2
J
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[i] in
UnsafeChars) or (ASrc[i] >= #$80) or (ASrc[1] < #32)" portion of the code.

"Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in message
news:Xn**********************************@207.46.2 48.16...
"J" <se******@sendspam.com> wrote in
news:vo************@corp.supernews.com:
I would like to translate the following to C#, and am wondering
what is the simpliest way to replace the "( if (ASrc[i] in
UnsafeChars) or (ASrc[i] >= #$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[i] in UnsafeChars) or (ASrc[i] >= #$80) or
(ASrc[1] < #32)
then
Result := Result + '%' + IntToHex(Ord(ASrc[i]), 2)
else
Result := Result + ASrc[i];
end;
end


J,

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

Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/

Nov 15 '05 #3
J <se******@sendspam.com> wrote:
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[i] in
UnsafeChars) or (ASrc[i] >= #$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[i] < 32 || ASrc[i] > 0x80)
....

(I'm assuming that #$80 = 128 here, in other words #$ means "hex".)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
>I would like to translate the following to C#, and am wondering what is the
simpliest way to replace the "( if (ASrc[i] in UnsafeChars) or (ASrc[i] >=
#$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

Nov 15 '05 #5
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[i];

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

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

else

result += c;

}

return result;

}

"J" <se******@sendspam.com> wrote in message
news:vo************@corp.supernews.com...
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[i] in
UnsafeChars) or (ASrc[i] >= #$80) or (ASrc[1] < #32)" portion of the code.

"Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in message
news:Xn**********************************@207.46.2 48.16...
"J" <se******@sendspam.com> wrote in
news:vo************@corp.supernews.com:
I would like to translate the following to C#, and am wondering
what is the simpliest way to replace the "( if (ASrc[i] in
UnsafeChars) or (ASrc[i] >= #$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[i] in UnsafeChars) or (ASrc[i] >= #$80) or
(ASrc[1] < #32)
then
Result := Result + '%' + IntToHex(Ord(ASrc[i]), 2)
else
Result := Result + ASrc[i];
end;
end


J,

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

Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/


Nov 15 '05 #6
"J" <se******@sendspam.com> wrote in
news:vo************@corp.supernews.com:
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[i] in UnsafeChars) or (ASrc[i] >=
#$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.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: PythonMan | last post by:
Any Software can change Python source code to Delphi ? thx --
6
by: Erva | last post by:
Hi, Is there someone who has moved from Delphi to VS.NET? I'am using Delphi currently but seriously considering to moving VS.NET. I would like to hear if someone has already done that, is it...
7
by: Steven T. Hatton | last post by:
Is there anything that gives a good description of how source code is converted into a translation unit, then object code, and then linked. I'm particularly interested in understanding why putting...
48
by: Edwin Quijada | last post by:
Hi !! Everybody I am developing app using Delphi and I have a question: I have to save pictures into my database. Each picture has 20 o 30k aprox. What is the way more optimus? That 's table will...
3
by: lukeharpin | last post by:
Currently I have been developing applications in Delphi 7. Recently I meet up with a friend of mine who previously developed in Delphi, from version 1 - 7. When Delphi 8 .net was release he found...
38
by: Arjang | last post by:
http://www.codeproject.com/useritems/CSharpVersusVB.asp
1
by: Thomas Due | last post by:
Hi, I manage an rather old application in which we have some fairly complex (ugly) Delphi code. This is Delphi 6 we're talking about. Among all this Delphi code there is method for formating a...
4
by: =?Utf-8?B?ZGF2ZWJ5dGhlc2Vh?= | last post by:
Dear List, We have a large application written in Delphi. I am writing a mobile app written in .NET (C#) which reads and writes into the Delphi applications Oracle database via a .NET Web...
11
by: gnuist006 | last post by:
Is there a Delphi equivalent in the C world or Scheme/LISP world ? Recently, Delphi is in resurgence. In Russia people are using like crazy. For example, Bolega has written a free image...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.