|
I need to implement the simple encryption function below in C#. Anyone that knows Delphi and C# able to help?
function EncryptStr(const S: String; Key: Word): String;
var
I: Integer;
const
C1 = 53761;
C2 = 32618;
begin
Result := S;
for I := 1 to Length(S) do begin
Result[I] := char(byte(S[I]) xor (Key shr 8));
Key := (byte(Result[I]) + Key) * C1 + C2;
end;
end;
|
|
|
|
|
In C# characters are 2 bytes wide so probably want to first convert the string into an array of bytes. At that step, you have to choose which encoding you want to use. Once you have such an array, you can then apply the above algorithm on it. But be aware that the resulting array of bytes might not be valid for the selected encoding and thus it might be somewhat problematic to return a string. Also because of encoding, the final length might also be different. So you have to know exactly what you want to do and if you want to properly support any valid string or only those that are limited to the ANSI character set.
Philippe Mori
|
|
|
|
|
Not true. Characters are only two bytes wide if you use UTF16 encoding (and I've never seen anyone do so). In UTF8, some characters are 1, some are 2 and some are more. You can also force it to read bytes as ANSI characters using Encoding.Default.
I agree though that in .Net it's better to do algorithms like this on byte arrays not character ones, if possible.
|
|
|
|
|
Sorry, but Philippe is right.
.NET always use UTF-16 encodings for strings. And as he correctly mentions you can select the encoding to use when converting to/from a byte array. You can not select encoding to use in a string.
|
|
|
|
|
I'm not sure that's actually true either (you can put Unicode code page 2, i.e. UTF32, characters in a .Net string, right?). But since you can never get at the internal representation of a string anyway, it's irrelevant ... the only time byte count matters is when you convert a string to bytes (or vice versa), and the default for that is definitely UTF8. How the string is actually stored internally is implementation dependent and probably depends on what's actually in the string.
|
|
|
|
|
UTF-32 is just again another encoding of the Unicode characterset. It does not contain any character that can't be represented by UTF7, UTF-8, or UTF-16. But unlike these encodings UTF-32 has a fixed length of all characters (4 bytes) where for example UTF-16 use two or four bytes per character.
I seriously doubt the encoding of the string used for the in memory representation of the string depends on what is in it. If an implementation stored anything else than UTF-16, it would have to reencode every time you indexed a character in the string (including doing substrings, regex, etc). Sure it is possible, but it would be a lot of complexity added for no reasonable gain.
|
|
|
|
|
In C#, all characters are in Unicode.
Philippe Mori
|
|
|
|
|
You know, that was what Ridley Scott was going to run with before he went with "In space no one can hear you scream"?
|
|
|
|
|
Honestly, this is pretty trivial. If you don't know Delphi, how do you know this function does what you want? And if you don't know C# well enough to translate it, why are you writing a C# app?
The only subtlety is that the algorithm is working on bytes, so you should use a byte[] not a string, and the resulting byte stream might well not be translatable to a UTF8 string at all.
|
|
|
|
|
Hi All,
Question I have a string of Data that I am going to split with the method :
string[] Values = FileData.Split(delimeterCharFurther);
delimeterCharFurther are
char[] delimeterCharFurther = { '<', '>' };
I need to check the string before I split it that all the valid data is surrounded by < and >. I was thinking of a variable
for (int a = 0; a <= FileData.Length; a++)
{
Location = FileData.IndexOf("><");
MessageBox.Show(Location.ToString());
} but if I'm right that will only find the first, one way to do it I'm guessing is to search for a > without a following
|
|
|
|
|
IndexOf is overloaded, therefore location=string.IndexOf(subString, location+1); if often used in a loop for such purposes.
|
|
|
|
|
Hi Glenn,
Not sure what 'valid' means : what about "<a<b>>" ?? you should get "a","b"
If so 'split' is probably not the path to the solution.
But if not (i.e. <a<b>> is invalid )
why not
string[] Values = FileData.Split('<');
first
then check values that are ending with a '>'
Regards
|
|
|
|
|
Thanks for that, I think 'valid' was my typing as thinking the data is (should be) enclosed in < & > but in transmission some times it shows up as Data not (or even D!"" random ASCII) if it does throw 'em away and carry on. My think was along the lines of declare an int pointer increment over the string, then I remembered it was Windows and C#, not DOS & Turbo C!!
Glenn
|
|
|
|
|
Or use a Regular Expression:
@"<(?'Data'[^>]*?>"
|
|
|
|
|
Hellow every body I can't send mesg by asp.net coz this exception accore the operation time out
|
|
|
|
|
A couple of points.
1. Without showing us any code whatsoever, we couldn't even begin to guess where it's going wrong.
2. What do you mean by accore? I appreciate English isn't your first language, but I'm not sure what word that's meant to be.
|
|
|
|
|
Pete O'Hanlon wrote: What do you mean by accore?
I suspect it should be occur.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
|
Firstly, this really belongs in the ASP.NET forum.
Secondly, I think some of the answers to this question[^] may be relevant.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
|
Timeout - most probably a network issue where you are unable to connect to the server.
Make sure your network is up and you are able to connect to the mail server.
|
|
|
|
|
thanks for u Abhinav S the problem is connect to the server
|
|
|
|
|
Hello,
I'm tring go apply blending with transparency, I have a transparent image and another full image.
When I apply Multiple Blending option the transparent side becomes black, is there an option to make them transparent or white?
|
|
|
|
|
OK It's just about using PixelFormat.Format32bppArgb
|
|
|
|
|
Who can help me with an idea (title) for a dissertation (final work to master)? The preferred can be done in C #.
|
|
|
|