Gan
Gan

Reputation: 164

Byte Array to Decimal Java

I have a Individual class that has the follow code

public class Individual {

    static int DNA_lenght = 64;
    private int fitness = 0;

    private byte[] genes = new byte[DNA_lenght];

    public void initialise_individual(){

        for(int i = 0; i < DNA_lenght; i++){

            byte gene = (byte) Math.round(Math.random());
            genes[i] = gene;
        }

    }
}

how can I write a method to translate the whole value of the individual into a decimal value?

Upvotes: 2

Views: 3569

Answers (3)

jmiserez
jmiserez

Reputation: 3109

You seem to want to create a random array of bytes. But what you are currently doing is generating a random array of 0 and 1 bytes (not bits). All of your bytes (=8 bits) are either 00000000 or 00000001. What you really want is a byte in the range 0-255. You should use Random.nextBytes(byte[] genes) to create your genes.

Generates random bytes and places them into a user-supplied byte array. The number of random bytes produced is equal to the length of the byte array.)

Then you want to convert this back into a decimal. However, as soon as you have more than 4 bytes, the values will no longer fit into a 32-bit (=4 byte) integer. If you have more than 8 bytes, they won't fit into a 64-bit integer etc.

For conversion, I'd use the BigInteger class. It takes care of the conversion for you, so you don't have to deal with endianness.

So, assuming you want a random gene of 64 bits, you do this as follows:

public static void main( String args[] ){
      // create random object
      Random r = new Random();

      int numBits = 64;
      int numBytes = numBits/8;

      // create the byte array
      byte[] genes = new byte[numBytes];

      //fill it
      r.nextBytes(genes);

      //print it
      System.out.println("byte array: " + genes);

      //now convert it to a 64-bit long using BigInteger          
      long l = new BigInteger(bytes).longValue();

      //print it
      System.out.println("long: " + l);
  }      

Of course, you could also generate a random long value directly, by using:

Random r = new Random();
long longGenes = r.nextLong();

And convert that to a byte array if you really need one:

byte[] genes = new BigInteger(longGenes).toByteArray();

Upvotes: 2

azz
azz

Reputation: 5930

Firstly, why is length spelled lenght?

Secondly, this will fill the array with zeros, which I presume is not what you want.

    for(int i = 0; i < DNA_lenght; i++){

        byte gene = (byte) Math.round(Math.random());
        genes[i] = gene;
    }

Instead try something like:

    genes = new byte[64];
    new Random().nextBytes(genes);

Finally, you cannot uniquely convert a byte array of length 64 (so 512 bits) into a decimal value (double is only 64 bits).

Update

After reading the question's comments, I am lead to believe you want convert 64 bits into a decimal value. That is possible, and can be simply done with new Random().nextLong(), or if you insist on using the byte data type:

genes = new byte[8];
// fill with random values
new Random().nextBytes(genes);
// convert to long
long decimalValue;
for (int i = 0; i < 8; i++) {
    decimalValue <<= 8;
    decimalValue += genes[i] & 0xff;
}

Upvotes: 1

kan
kan

Reputation: 28951

I think BigInteger is more appropriate. There is a random generation such as constructor java.math.BigInteger.BigInteger(int numBits, Random rnd). Also there are constructor from byte array and toByteArray method.

Upvotes: 0

Related Questions