CodeGust
CodeGust

Reputation: 844

Java generate code byte 256*256 & 0xff

I need to generate a 3-bytes code (like A502F1). I am given a criteria:

1st byte is (serialCodeNumber / (256*256) ) & 0xFF

2nd is (serialCodeNumber / 256) & 0xFF

3th is (serialCodeNumber) & 0xFF

serialCodeNumber is a sequence 1-0xFFF

What does that mean!?

I would generate it like this:

String codeNum = new BigInteger(256, random).toString(16).toUpperCase().substring(0, 6);

But what is the right way of doing it as the requirement says?

Upvotes: 0

Views: 950

Answers (1)

Ceiling Gecko
Ceiling Gecko

Reputation: 3186

I'm not quite sure what is meant by the serialCodeNumber, since if it is later on divided by 65025 it has to be a considerably larger number than 0xFFF (which is 4095) for it to make any reasonable sense.

But let's take a look at the conditions, they would all make sense once you are accustomed to the bitwise AND operator. A good read is available here on how it works but the meat of the matter from that question in my opinion is this sentence by Markus Jarderot:

The result is the bits that are turned on in both numbers.

Since in your conditions you have & 0xFF and 0xFF is 255, or in binary it's 11111111 the first eight bits that are all turned on. This is a neat trick to just retrieve only the first 8 bits of any number. And as we all know 8 bits make up a byte. (Are you starting to see where this all is coming together now?)

As for the conditions before the & 0xFF, some might recognize them as bit shift operations hidden behind divisions.

(serialCodeNumber / (256*256)) is equivalent to (serialCodeNumber >> 16)

and

(serialCodeNumber / 256) is equivalent to (serialCodeNumber >> 8)

But that is not that important in this case.

So the first condition takes the serialCodeNumber divides it by 65025 (256*256) and then looks at the 8 right most bits and ignores any other, from those 8 bits it constructs a byte.

In Java you can pretty much just write the condition as it is:

byte myFirstByte = (byte) ((serialCodeNumber / (256*256)) & 0xFF);

The other conditions aren't much different:

byte mySecondByte = (byte) ((serialCodeNumber / (256)) & 0xFF);

and

byte myThirdByte = (byte) ((serialCodeNumber) & 0xFF);

Once you have all three of your bytes, I'm assuming you need to convert them to a hex String. So I'll add them into a byte array.

byte[] myArray = {myFirstByte,mySecondByte,myThirdByte};

And borrow some method on how to convert byte arrays to HEX strings from this question.

String codeNum = bytesToHex(myArray);

And the result will look something like this:

F03DD7

EDIT:

Since you have to generate a serial number that has to be up to 6 bytes in value, I'd recommend using a long number.

A 6 byte number will be anywhere from 1 to 281474976710655, so you probably need to generate one randomly.

First instantiate a Random object which you will be able to poll numbers from:

Random random = new Random();

Once you have that, poll a long from it for the range 1 to 281474976710655.

For this you can borrow KennyTM's answer from this question.

So you can then generate the number like so:

long serialCodeNumber = nextLong(random, 281474976710655L)+1L;

We add the +1L at the end since we want it to include the last number as well as start from 1 instead of 0.

If you ever need to show a HEX string of the serialCodeNumber you can then just call:

String serialHex = Long.toHexString(serialCodeNumber);

But make sure to add any additional "0"s at the left side based on the length of the string so that it is 6-bytes = 12 characters long.

Upvotes: 1

Related Questions