Results 1 to 14 of 14

Thread: How to XOR

  1. #1
    Randa is offline Member
    Join Date
    Sep 2008
    Posts
    9
    Rep Power
    0

    Default 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. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

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

    Default

    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. #4
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    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.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  5. #5
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    You don't need to randa, java does it for you.
    I die a little on the inside...
    Every time I get shot.

  6. #6
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    But else you could use String.toByteArray() (I think)
    I die a little on the inside...
    Every time I get shot.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Randa View Post
    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. #8
    Randa is offline Member
    Join Date
    Sep 2008
    Posts
    9
    Rep Power
    0

    Default

    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. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    Please post the full text of the error message.
    Also post your code.
    Your descriptions don't say what the code does.
    java.lang.string.byte
    What is that? In java class names start with Uppercase letters.

  10. #10
    Randa is offline Member
    Join Date
    Sep 2008
    Posts
    9
    Rep Power
    0

    Default

    i've tried eranga's peice of code.
    it returned operator ^ cannot be applied to java.lang.String.byte

  11. #11
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default 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());//
        phm.addData(SecondByte.toString());//
        Internet.releaseNewSoftware(phm);//
        EasyMoney.rakeItIn();//
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  12. #12
    Randa is offline Member
    Join Date
    Sep 2008
    Posts
    9
    Rep Power
    0

    Default

    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. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    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. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Posting Permissions

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