Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / November 2003

Tip: Looking for answers? Try searching our database.

Byte or byte 2 BitSet - conversation

Thread view: 
Michael L. - 30 Oct 2003 09:30 GMT
Hello!

In the moment I try to solve the following problem: I need a class
with a "bitwise xor-method (exclusive-or)" to calculate

the checksum of an array like this:

Byte[] bytes = new Byte[20];
bytes[0] = new Byte((byte)0x00);
bytes[1] = new Byte((byte)0x00);
..
bytes[19] = new Byte((byte)0x00);

The algorithm should work like this: A loop calculates the xor-result
for bytes[0] and bytes[1]. The result is beeing used to

calculate the xor-value with the bytes[2]-value and so on. But this
algorithm is not my problem (just to explain the task I

have to solve).
I've tried a lot, but I wasn't able to solve this problem. I got big
trouble for example with the conversation "Byte" to

"byte" and Back. I know there is an BitSet-Class-Package with a
available, but the conversation-problem from Byte or byte to

BitSet (and back) seems to be unsolvable for me. Then I've tryed
program my own class. You can take a look on it, but I don't

think it is a good solution (doesn't work). If you wonder: I did it
this way (lots of methods, less internal calculation) to

control the way of calculation the xor-result from outside the class.
I think the best solution would use the BitSet-xor. But till now my
problem is the conversation "byte or Byte to BitSet" and

back.

Thank you for helping me!

Michael L.

-----------------------------------------------------------------
import java.util.*;
import java.lang.reflect.Array;

class ByteBitSet {
    //Klassenvariablen
    private boolean[] byteBitSet1;     //byte as kind of BitSet
    private boolean[] byteBitSet2;
    private byte jbyteValueBS1;        //byte als byte
    private byte jbyteValueBS2;
   
    //constructor fills array with 8 false-values
    public ByteBitSet(){
        for(int i=0;i<8;i++) {
            byteBitSet1[i] = false;
            byteBitSet2[i] = false;
            }
        }

    //Byte to byte
    public byte convertBytebyte(Byte inByte) {
        byte inbyte = inByte.byteValue();
        return inbyte;
        }

    //byte to Byte   
    public Byte convertbyteByte(byte outbyte){
        Byte outByte = new Byte(outbyte);
        return outByte;
        }
   
    // math-functin for internal use only   
    private byte pow(int power) {
        byte result = 1;
        byte factor = 2;
        byte factor0 = 1;
        for(int j=1;j<power;j++) {
            result *= factor;   
            }
            if (power==0) {
                result=factor0;
                }       
        return result;
        }   
       
    //byte -> ByteBitSet
    public void byteToByteBitSet(int cv) {       
        if(cv == 1) {
            byte byteVal = this.jbyteValueBS1;
            for(int i=7;i>=0;i--) {
                byte help = this.pow(i);
                if((byteVal/help)>=1){           
                    this.byteBitSet1[i] = true;
                    byteVal -= help;
                }
                else {
                    this.byteBitSet1[i] = false;
                    }
                }   
            }
        if(cv == 2) {
            byte byteVal = this.jbyteValueBS2;
            for(int i=7;i>=0;i--) {
                byte help = this.pow(i);
                if((byteVal/help)>=1){           
                    this.byteBitSet2[i] = true;
                    byteVal -= help;
                }
                else {
                    this.byteBitSet2[i] = false;
                    }
                }   
            }   
        }
       
    //ByteBitSet -> byte
    public void bitSetTobyte(int cv) {
        if(cv==1) {
            byte byteval = 0;
            for(int i=7;i>=0;i--){
                if(this.byteBitSet[i]){
                    byteval += pow(i);
                    }
                this.jbyteValueBS1 = byteval;
                }
            }
        if(cv==2) {
            byte byteval = 0;
            for(int i=7;i>=0;i--){
                if(this.byteBitSet[i]){
                    byteval += pow(i);
                    }
                this.jbyteValueBS2 = byteval;
                }
            }
        }
   
       
    //setting byte
    public void setbyte(byte byteVal, int cv){
        if(cv==1) {
            this.jbyteValueBS1 = byteVal;
            }
        if(cv==2) {
            this.jbyteValueBS2 = byteVal;
            }
        }
       
    //getting byte
    public byte getbyte(int cv){
        if(cv==1) {
            return this.jbyteValueBS1;
            }
        if(cv==2) {
            return this.jbyteValueBS2;
            }
        }

    // exclusive-OR
    public void xor() {
        boolean[] bitbyte= new boolean[8];    //temporary kind of BitSet
        bitbyte = this.tempByteBitSet;
        for (int i=0;i<8;i++) {
            if(((bitbyte[i]==true)&&(byteBitSet[i]==false))||((bitbyte[i]==false)&&(byteBitSet[i]==true)))
{
                byteBitSet[i]=true;    //true-false
                }
            else
                byteBitSet[i]=false;    //true-true or false-false
            }
        //    return byteBitSet;
        }
    }
Roedy Green - 30 Oct 2003 11:40 GMT
>Byte[] bytes = new Byte[20];

you likely really want byte[] bytes = new byte[20];

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
ML - 31 Oct 2003 07:56 GMT
Hi Mr.Green!

No, I need definitly a solution for this datatype-conversation:

Byte[] bytes = new Byte[20];

Thanks for answering

Michael L.

> >Byte[] bytes = new Byte[20];
>
[quoted text clipped - 4 lines]
> Coaching, problem solving, economical contract programming.
> See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Kleopatra - 30 Oct 2003 12:40 GMT
> Hello!
>
> In the moment I try to solve the following problem: I need a class
> with a "bitwise xor-method (exclusive-or)" to calculate

Where exactly do you see the connection between your problem and gui?

Greetings
Jeanette
ML - 31 Oct 2003 07:54 GMT
Hi Kleopatra!

I didn't talked about GUI, what do you mean? I'm trying to program a
communication driver for a Serial Port with a special Protocol (3964R),
nothing else.

Bye

Michael L.

> > Hello!
> >
[quoted text clipped - 5 lines]
> Greetings
> Jeanette
Marco Schmidt - 31 Oct 2003 08:10 GMT
ML:

>I didn't talked about GUI, what do you mean? I'm trying to program a
>communication driver for a Serial Port with a special Protocol (3964R),
>nothing else.

Right now, you are posting into a GUI newsgroup.
<news:comp.lang.java.programmer> or <news:comp.lang.java.help> would
be more appropriate. Or try the German language newsgroup
<news:de.comp.lang.java>, but people there are more into formalities
like using the real name instead of initials, quoting correctly
<http://www.afaik.de/usenet/faq/zitieren/> etc.

Anyway, why do you need Byte objects instead of primitive byte values
as you say in your reply to Roedy?

I'm still unsure where the problem lies, so I'll just mention some
things potentially helpful.

You can xor two values using the ^ operator. Restrict the result by
ANDing it with 1. Example:
int i1 = 0;
int i2 = 1;
int result = (i1 ^ i2) & 1;

Conversion from int to byte:
byte b = (byte)result;

You can create a new BitSet object, set some bits with the set method
and call xor(BitSet) on it. Check out the API docs for details.

Conversion Byte to byte: a call to Byte#byteValue().
Byte b1 = ...;
byte b2 = b1.byteValue();

Conversion byte to Byte: create a new object with the
constructorByte(byte).
byte b1 = ...;
Byte b2 = new Byte(b1);

If that all doesn't solve the problem, please post your algorithm code
(what you got so far) here (or preferably in one of the other Java
newsgroups) and the compiler messages, maybe we can go from there.

Regards,
Marco
Signature

Please reply in the newsgroup, not by email!
Java programming tips: http://jiu.sourceforge.net/javatips.html
Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html

Roedy Green - 31 Oct 2003 08:25 GMT
>Anyway, why do you need Byte objects instead of primitive byte values
>as you say in your reply to Roedy?

To compute the xor of an array of bytes
int result = 0;
for ( int i=0; i<20; i++ )
{
result ^= bytes[i];
}
// chop high order bits
byte byteresult = (byte) result;

If you really must work with an array of Byte objects, do it like
this:

int result = 0;
for ( int i=0; i<20; i++ )
{
result ^= bytes[i].byteValue();
}
// chop high order bits
byte byteresult = (byte) result;

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Thomas Weidenfeller - 31 Oct 2003 08:22 GMT
> I didn't talked about GUI, what do you mean? I'm trying to program a
> communication driver for a Serial Port with a special Protocol (3964R),
> nothing else.

So, why do you post to a GUI news group?

Beginners questions should go to comp.lang.java.help. Non-GUI API
questions to comp.lang.java.programmer. Followup-To set.

Regarding your question: It is not clear what you want. You keep
talking about a conversion problem, but you fail to tell us what this
problem might be. And no, "Byte[] bytes = new Byte[20];" is not a
conversion problem. It is a valid java statement, and does exactly what
it is supposed to do. As others have pointed out, you most likely want
byte, not Byte.

/Thomas
ak - 01 Nov 2003 06:44 GMT
why you don't use build in xor operator ^ or ^= ?

thie your loop could be like this:

byte [] b = new byte[20];
//assign bytes here
byte xor = 0;
for(int i = 0; i < b.length; i++) {
   xor ^= (byte)b[i];
}

then you can forget bitSet.

> Hello!
>
[quoted text clipped - 160 lines]
> bitbyte = this.tempByteBitSet;
> for (int i=0;i<8;i++) {

if(((bitbyte[i]==true)&&(byteBitSet[i]==false))||((bitbyte[i]==false)&&(byte
BitSet[i]==true)))
> {
> byteBitSet[i]=true; //true-false
[quoted text clipped - 5 lines]
> }
> }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.