473,324 Members | 2,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

logical operations on characters

Hello,

i have following source, to be translatet into C#

for (i = 1; i < len; i++)
key[i] = lock[i] 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?
Nov 16 '05 #1
4 1987
Dirk Reske <_F*******@gmx.net> wrote:
i have following source, to be translatet into C#

for (i = 1; i < len; i++)
key[i] = lock[i] 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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
I have to calculate a key out of a string, to log into a server.
the characters dont have to be printable...

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

"Jon Skeet [C# MVP]" <sk***@pobox.com> schrieb im Newsbeitrag
news:MP**********************@msnews.microsoft.com ...
Dirk Reske <_F*******@gmx.net> wrote:
i have following source, to be translatet into C#

for (i = 1; i < len; i++)
key[i] = lock[i] 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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
Dirk Reske <_F*******@gmx.net> wrote:
I have to calculate a key out of a string, to log into a server.
the characters dont have to be printable...

perhaps: key[i] = (int)lock[i] ^ (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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
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[i] =(byte) (bytLock[i] ^ 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" <_F*******@gmx.net> wrote in message
news:uX**************@TK2MSFTNGP14.phx.gbl...
I have to calculate a key out of a string, to log into a server.
the characters dont have to be printable...

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

"Jon Skeet [C# MVP]" <sk***@pobox.com> schrieb im Newsbeitrag
news:MP**********************@msnews.microsoft.com ...
Dirk Reske <_F*******@gmx.net> wrote:
i have following source, to be translatet into C#

for (i = 1; i < len; i++)
key[i] = lock[i] 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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 16 '05 #5

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

Similar topics

4
by: muser | last post by:
Can anyone run this program through their compiler or if they can see a logical error please point it out. I have my tutor working on it at the moment but I would rather a less ambigious response...
80
by: Christopher Benson-Manica | last post by:
Of course one can get the effect with appropriate use of existing operators, but a ^^ operator would make for nice symmetry (as well as useful to me in something I'm working on). Am I the only one...
8
by: siliconwafer | last post by:
Hi All, If I open a binary file in text mode and use text functions to read it then will I be reading numbers as characters or actual values? What if I open a text file and read it using binary...
3
by: Filip Kratochvil | last post by:
Hello all, In VB6 I'm able to do the following: If (intValue And 64) = 64 Then ... do something here ... End If
8
by: Chua Wen Ching | last post by:
Hi, I had some beginner questions. Do we need Shift << >> or Logical AND OR XOR operator in our daily programming? I am not sure why i need to use it? I had some samples of c# codes using it. ...
17
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some...
1
by: AinJul Rose | last post by:
Hi, I'm in desperate need of help. My exam is in 2 days and I dont understand anything covering the logical operations and Loops. The format for my exam is simply to convert a few things into C...
3
by: Sumitava Mukherjee | last post by:
Hi all, I am a novice programmer in Python. Please could you explain me the results (regarding logical operators). I get this: True
0
by: tabassumpatil | last post by:
Please send the c code for: 1.STACK OPERATIONS : Transfer the names stored in stack s1 to stack s2 and print the contents of stack s2. 2.QUEUE OPERATIONS : Write a program to implement...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.