1. Member
Join Date
Sep 2008
Posts
9
Rep Power
0

## 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

2. Convert two variable in to bit number and XOR. Nothing more that that.

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?

3. Member
Join Date
Sep 2008
Posts
9
Rep Power
0
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

4. 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.

5. Senior Member
Join Date
Aug 2008
Posts
384
Rep Power
9
You don't need to randa, java does it for you.

6. Senior Member
Join Date
Aug 2008
Posts
384
Rep Power
9
But else you could use String.toByteArray() (I think)

7. 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?

8. Member
Join Date
Sep 2008
Posts
9
Rep Power
0
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

9. Please post the full text of the error message.
Your descriptions don't say what the code does.
java.lang.string.byte

10. Member
Join Date
Sep 2008
Posts
9
Rep Power
0
i've tried eranga's peice of code.
it returned operator ^ cannot be applied to java.lang.String.byte

11. ## some ideas .....

Java 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());//
Internet.releaseNewSoftware(phm);//
EasyMoney.rakeItIn();//```

12. Member
Join Date
Sep 2008
Posts
9
Rep Power
0
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

13. 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];

14. 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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•