4

I am looking for the most efficient way to convert array of int to array of byte and vice versa.

I want to write a huge number of int array and String to files so I can read them efficiently. I think the best way to save space and decrease reading time is to store them as byte. Am I right?

I read the following link but is there any better way?

Convert integer into byte array (Java)

Community
  • 1
  • 1
user3487667
  • 519
  • 5
  • 22

4 Answers4

4

integers to bytes

I don't think there is a much better or more effective way then directly shifting the bytes/bits.

You will probably need to loop over the toBytes(..) method mentioned in the question you linked:

byte[] toBytes(int i)
{
  byte[] result = new byte[4];

  result[0] = (byte) (i >> 24);
  result[1] = (byte) (i >> 16);
  result[2] = (byte) (i >> 8);
  result[3] = (byte) (i /*>> 0*/);

  return result;
}

Like this:

for (int i = 0; i < integers.length; i++)
{
   bytes[i] = toBytes(integers[i]);
}

bytes to integers

For bytes to integers you can do simple multiplication and adding which would probably be the fastest. Something like:

for (int i = 0; i < integers.length; i++)
{
   integers[i] = ((int) (bytes[i] << 24)) + ((int) (bytes[i+1] << 16)) + ((int) (bytes[i + 2] << 8)) + ((int) (bytes[i + 3]));
}

Also see

Community
  • 1
  • 1
flotothemoon
  • 1,882
  • 2
  • 20
  • 37
  • Storing `int`s as 4 `byte`s doesn't save you any space efficiency (both solutions are 32 bits). I believe the OP was asking about storing his `int`s as single `byte`s to save space. Regardless, your solution is the most efficient way to perform the conversion *without loss of data*. – Luke Willis Jul 31 '14 at 15:51
  • @LukeWillis Thanks, and yes, you are right, this is probably the most efficient way without data loss, but I am not sure why someone would want to save ints as (single!) bytes.. ;) (You could save them as bytes right away if you only have -127 to 128) – flotothemoon Jul 31 '14 at 17:53
  • @1337 indexing is an example which prefer store a byte instead of 4 byte because of saving space. Also it is a reason I want to convert array of _int_ to array of _byte_. – user3487667 Jul 31 '14 at 19:02
  • @user3487667 Hmm.. yeah probably. Interesting, thanks :) – flotothemoon Jul 31 '14 at 19:25
  • @flotothemoon I believe reading the bytes should be bytes[4*i], bytes[4*i+1] and so forth – Alessandro Teruzzi Oct 05 '22 at 13:46
2

With regards to efficiency, the size of the file to be written and read will dictate which approach to use. You might need to simply run a few timed tests to see which approach will work best for what you are doing.

Another option (for smaller files) is to utilize Files.readAllBytes() and Files.write().

Bret
  • 2,283
  • 4
  • 20
  • 28
2

In Java, a byte is 8 bits, while an int is 32 bits.

To convert each of your ints to a single byte without loss of data, you need to ensure all your numbers are in the range -128 to 127 inclusive.

If they are in this range, then you should just store them as bytes in the first place (if this is not possible, there are ways to do the conversion already discussed).

If they are not in this range, then you shouldn't be converting them to bytes at all because it will force your numbers into the range, and thus you will lose a lot of data.

Alternatively, you could use short, as that would be 16 bits (giving you the range -32,768 to 32,767 inclusive).

But if you can't ensure that your numbers will be within these ranges, then you will need to use int.


Note: You can store each int as 4 bytes or as 2 shorts, but each number would still be 32 bits, so you're not gaining any space efficiency by doing so.


You can read more about Java's primitive types here.

Luke Willis
  • 8,429
  • 4
  • 46
  • 79
1

Java provides an API that does that for you much more efficiently. Look at http://docs.oracle.com/javase/7/docs/api/java/io/DataOutputStream.html. Also, if you don't want to open the files with other application, another efficient solution is to use an http://docs.oracle.com/javase/7/docs/api/java/io/ObjectOutputStream.html that will serialize an entire object without having to convert it manually.

Also, check this answer: Fastest way to write an array of integers to a file in Java?

Community
  • 1
  • 1
rhobincu
  • 906
  • 1
  • 7
  • 22