-1

This does not seem to be appropriate. Is there a way to create a hexadecimal array?

 float[] bitBytes = {0x80, 0x40, 0x20, 0x10, 8, 4, 2, 1};

 for (int k = 0; k < alot; k++) {
  BitSet.set(increment++, ((array[k] & (bitBytes[k%8]& 0xff)) != 0));
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
user1198289
  • 637
  • 1
  • 5
  • 14
  • Please have a look at [Literal Syntax For byte arrays using Hex notation..?](http://stackoverflow.com/questions/19450452/how-to-convert-byte-array-to-hex-format-in-java) – Braj Apr 13 '14 at 18:31
  • 2
    How does a "hexadecimal array" differ from a regular array? (And what do you expect to accomplish by assigning integers to a float array?) – Hot Licks Apr 13 '14 at 18:39
  • There doesn't appear to be a good reason to use `float` – Peter Lawrey Apr 13 '14 at 19:21
  • (1) Define 'does not seem to be appropriate'. (2) There is no such thing as a hexadecimal array. There are just arrays, possibly with intial values, which if present may be notated in hexadecimal in the source code. – user207421 Mar 13 '18 at 04:25

2 Answers2

0

byte[] biteBytes = new byte[8]; for (int j = 0; j < bitBytes.length; j++) { bitBytes[j] = (byte) (Math.pow(2,j));
}

user1198289
  • 637
  • 1
  • 5
  • 14
  • But why assign `byte` values to `float` array elements? – Hot Licks Apr 13 '14 at 19:09
  • @owlstead - There's no `byte` to "per". – Hot Licks Apr 13 '14 at 20:24
  • 1
    @HotLicks I tried to answer you in the hope that this would clear things up for you. The floats are used in the original question. It is not done to create a long conversation on the answer of user1198289, and I apologize for creating one here. I've removed my comments, I would recommend you do the same. – Maarten Bodewes Apr 13 '14 at 20:54
0

Hexadecimals is a representation of bytes as a String, or at least an array of characters. It is mainly used for human consumption, as it is easier to see the bit value of the bytes.

To create a byte array containing byte values, you can use the following construct:

final byte[] anArray = { (byte) 0x10, (byte) 0x80 };

The cast to byte - (byte) - is really only required for values of 0x80 or over as bytes are signed in Java and therefore only have values ranging from -0x80 to 0x7F. Normally we only deal with unsigned values though, so we need the cast.

Alternatively, for larger strings, it can be useful to simply supply a hexadecimal string to a decoder. Unfortunately the idiots that have thought out the standard API still haven't defined a standard hexadecimal codec somewhere in java.lang or java.util.

So you can use another library, such as the Apache codec library or a self written function. Stackoverflow to the rescue.

Convert a string representation of a hex dump to a byte array using Java?

If you want to have a BitSet of the values in the byte array, please use BitSet.valueOf(byte[])

Community
  • 1
  • 1
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263