logical operations on characters

  • Thread starter Thread starter Dirk Reske
  • Start date Start date
D

Dirk Reske

Hello,

i have following source, to be translatet into C#

for (i = 1; i < len; i++)
key = lock xor lock[i-1];

key and lock are strings
in C# xor = ^

but in c# i cannot apply the ^ operator on characters.
How can I solve this?
 
Dirk Reske said:
i have following source, to be translatet into C#

for (i = 1; i < len; i++)
key = lock xor lock[i-1];

key and lock are strings
in C# xor = ^

but in c# i cannot apply the ^ operator on characters.
How can I solve this?


Well, XOR isn't really a logical operation on characters. While you
*can* do a bitwise XOR on the code points, there's no guarantee that
you'll get back a character which will be printable or anything like
that. It's generally a bad idea to treat character data as if it was
binary data.

What is the above used for?
 
I have to calculate a key out of a string, to log into a server.
the characters dont have to be printable...

perhaps: key = (int)lock ^ (int)lock[i-1]; ????

Jon Skeet said:
Dirk Reske said:
i have following source, to be translatet into C#

for (i = 1; i < len; i++)
key = lock xor lock[i-1];

key and lock are strings
in C# xor = ^

but in c# i cannot apply the ^ operator on characters.
How can I solve this?


Well, XOR isn't really a logical operation on characters. While you
*can* do a bitwise XOR on the code points, there's no guarantee that
you'll get back a character which will be printable or anything like
that. It's generally a bad idea to treat character data as if it was
binary data.

What is the above used for?
 
Dirk Reske said:
I have to calculate a key out of a string, to log into a server.
the characters dont have to be printable...

perhaps: key = (int)lock ^ (int)lock[i-1]; ????


How are you going to transmit the characters though? They may not even
be assigned Unicode characters after doing the above.

I think it's likely to be far better to convert the characters into
binary data *first* (using an Encoding) and then perform the XOR
operation on the binary data. If this is implementing an existing
specification, it should specify all this in terms of binary data
anyway, to be honest.
 
public class MyClass
{
public static void Main()
{
string strLock = "Hello World";
int len = strLock.Length;
System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();

byte[] bytLock = ascii.GetBytes(strLock);

byte[] key = new byte[len];
for (int i = 1; i < len; i++)
key =(byte) (bytLock ^ bytLock[i-1]);
// note this leave key[0] unassigned. Are you sure you want that?

// key is not guaranteed to contain codes for good ASCII characters,
// so the following probably won't work right.
// You probably really want the byte array "key"
string strKey = ascii.GetString(key);

Console.WriteLine(strKey);
}
}



--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
Dirk Reske said:
I have to calculate a key out of a string, to log into a server.
the characters dont have to be printable...

perhaps: key = (int)lock ^ (int)lock[i-1]; ????

Jon Skeet said:
Dirk Reske said:
i have following source, to be translatet into C#

for (i = 1; i < len; i++)
key = lock xor lock[i-1];

key and lock are strings
in C# xor = ^

but in c# i cannot apply the ^ operator on characters.
How can I solve this?


Well, XOR isn't really a logical operation on characters. While you
*can* do a bitwise XOR on the code points, there's no guarantee that
you'll get back a character which will be printable or anything like
that. It's generally a bad idea to treat character data as if it was
binary data.

What is the above used for?
 
Back
Top