0

I've started learning a little java, and am trying to accomplish what is likely a very simply task but I'm struggling with it.

Say I have a byte array:

byte[] test = {(byte) 0x0a, (byte) 0x01, (byte) 0x01, (byte) 0x0b};

and I want to change test[3], the last value which is currently the number 11 (0b), to something random.

Random generator = new Random();

int newTest3 = generator.nextInt(255);

So, now I have some random number in newTest3. I want to convert this to hex (FF) and then place that into the last element of test, or test[3].

I couldn't find much to help me on this, and I literally just picked up java a couple hours ago, so any help would be outstanding!

Thanks in advance :)

1
  • Converting Int to hex doesn't have any sens. Hex, Decimal and binary are representation of int. You can't store a hex value. Byte despite the name is just a number between -128, 127. Commented May 15, 2012 at 23:01

2 Answers 2

3

I don't see why you go over so much trouble. Use explicit cast as you are already doing :) when you write:

byte[] test = {(byte) 0x0a, (byte) 0x01, (byte) 0x01, (byte) 0x0b};

0x0a is actually an int which you explicitly cast to a byte. You could do the same with newTest3.

test[3] = (byte)  newTest3;

Notice that this kind of cast usually involve loss of data since byte is just 8bits and int is 32bits. so for example (FFFFFFFF would be cast to FF).

Sign up to request clarification or add additional context in comments.

5 Comments

Since he's using Random.nextInt(int) the output of the call is guaranteed to be from 0 to 255, so there's nothing being lost.
Taking what Quantum just said into account, that makes the answer perfect! Thanks much to both of you, as well as the other answer givers on here :)
@QuantumMechanic to avoid data loss, he should use 'generator.nextInt(255)-128;' Don't forget that byte is a signed 8bit number
He won't lose any info. Remember than signed vs. unsigned is only in how you interpret the bit pattern, not in the bit pattern itself. If you have byte b1 = (byte) -1; and byte b2 = (byte) 255 you will find out that (b1 == b2) is true.
There is dataloss if you map 255 to -1 :) Unless that's what you want your program to do.
2

Use a byte buffer

// elsewhere
import java.nio.ByteBuffer;

byte[] arr = new byte[4];
ByteBuffer buf = ByteBuffer.wrap(arr);
buf.putInt(generator.nextInt(255);

4 Comments

shouldn't it be buf.put((byte)generator.nextInt(255)); ?
Hey! I gave that one a little test try and ran into a bit of a problem: it said "cannot find symbol" for the buf.write method. Any suggestions?
@david That would be converting byte to a byte array, not an int to a byte array.
@GGati My apologies - I wrote a class recently that used write instead of put that I've been using. The proper method is "put int". But note that this will use 4 bytes per int (because an int has 4 bytes). IF you want just 1 byte per 'int', then this isn't the proper choice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.