How to XOR

• 09-14-2008, 12:58 PM
Randa
How to XOR
hi ,

I need help , on how to apply XOR on 2 byte variables .
the smalest var should be padded with 0.

any suggestions? it is really urgent.

thx .

Randa
• 09-14-2008, 01:43 PM
Eranga
Convert two variable in to bit number and XOR. Nothing more that that.

Quote:

byte x = 12; // 0000 1100
byte y = 10; // 0000 1010
System.out.println(x ^ y); // 0000 0110
JVM do the XOR operation for you. So what's your question here?
• 09-15-2008, 06:48 PM
Randa
Hi,
thx, but am new to java.
how to convert to bit??
i have 2 byte variables, and one is smalest than the other , so it should be padded with 0.
regards
• 09-15-2008, 08:23 PM
Nicholas Jordan
What do you mean convert to bit? Possible you wish to print the individual bits, but we need to know what it is you want to end up with and why.

Just studies is ok answer, could be several things.
• 09-15-2008, 08:24 PM
Supamagier
You don't need to randa, java does it for you.
• 09-15-2008, 08:24 PM
Supamagier
But else you could use String.toByteArray() (I think)
• 09-16-2008, 04:40 AM
Eranga
Quote:

Originally Posted by Randa
Hi,
thx, but am new to java.
how to convert to bit??
i have 2 byte variables, and one is smalest than the other , so it should be padded with 0.
regards

No need to convert into bits. Java do it for you. Did you check my code? I'm not converting any byte value into bits.

Do you know how to convert decimal value into binary value mathematically?
• 09-16-2008, 06:21 PM
Randa
Hi,
Eranga, your code returns : operator ^ cannot be applied to java.lang.string.byte

so....
i have 2 byte[] var to XOR , the result should be returned as byte[]
i tried converting to string , to have a sequence of 1s and 0s
it didn't work.

rgrds
• 09-16-2008, 06:40 PM
Norm
Please post the full text of the error message.
Your descriptions don't say what the code does.
Quote:

java.lang.string.byte
• 09-16-2008, 07:42 PM
Randa
i've tried eranga's peice of code.
it returned operator ^ cannot be applied to java.lang.String.byte
• 09-16-2008, 07:46 PM
Nicholas Jordan
some ideas .....
Code:

```/** * This code is a discussion of an opinion in a technical form. * There is no legitimate derivite use, and is known to be flawed. * Reader assumes the risk, of any kind and nature, in any time and place, * without burden of proof of jurisdition nor Snake Oil Certificate of Military Grade Encryption. */ import java.security.SecureRandom; class QuestionDemo {     private static byte[] BeuafordsBytes;//         // Dem darn demos, oh what a yo-yo!     SecureRandom random = SecureRandom.getInstance("SHA1PRNG");         BeaufordsBytes = random.generateSeed(2);     // xor operations here as directed by poster....     Byte FirstBite  = new Byte(BeaufordsBytes [0]);//     Byte SecondBite = new Byte(BeaufordsBytes [2]);//     PhoneHomeMessage phm = new PhoneHomeMessage(FirstByte.toString());//     phm.addData(SecondByte.toString());//     Internet.releaseNewSoftware(phm);//     EasyMoney.rakeItIn();//```
• 09-16-2008, 08:10 PM
Randa
this is the piece of code am working on
.........
byte[] entete2=new byte[12];
System.arraycopy(entete,0,entete2,0,entete.length) ;
System.arraycopy(bb,0,entete2,8,4);

messagesize=12+HEdatalength+HEdatalength2+cipherRn d.length;

smessage=new byte[messagesize];
System.arraycopy(entete2,0,smessage,0,entete2.leng th);
System.arraycopy(HE1,0,smessage,entete2.length,HE1 .length);
System.arraycopy(HE2,0,smessage,(entete2.length+HE 1.length),HE2.length);
System.arraycopy(cipherRnd,0,smessage,(entete2.len gth+HE1.length+HE2.length),cipherRnd.length);

at this point,i have to call a function with 2 parameters similar to smessage.
the function should apply the XOr and returns a byte [] value

i'll work on the code u provided
thx
• 09-16-2008, 11:58 PM
Norm
Quote:

operator ^ cannot be applied to java.lang.String.byte
When you get error messages, copy and paste the FULL text here. DO NOT EDIT them.

Your piece of code doesn't show why the ^ operator doesn't work.
Where is the code that Eranga showed you? It does an XOR on two bytes. If the bytes are in an array change the example to:
Given: byte[] b1 and byte[] b2

byte xorB = b1[i] ^ b2[i];
• 09-17-2008, 04:31 AM
Eranga
Yes, please sen your code here to see. In my code segment there is no any issues. May be you are using in wrong way. Sen the full code to see.