Table of Contents
Содержание
- 1. Overview
- 2. int to byte array
- 2.1 BigInteger
- 2.2 DataOutputStream
- 2.3 ByteBuffer
- 2.4 Shift operations
- 3. byte array to int
- 3.1 ByteBuffer
- 3.2 Shift operations
- 4. int array to byte array
- 5. byte array to int array
- 5. Conclusion
- Related Posts
- Check between two datetime if passed in Java/Andro >
- java.io.FileNotFoundException: /u/applic/wsadmin/geronimo217/geronimo8/var/catalina/work/TomcatWebContainer/SESSIONS.ser (Permis.
- Remove space between images added into a single pdf file with iText using java.
- 13 Answers 13
- Not the answer you’re looking for? Browse other questions tagged java arrays integer byte or ask your own question.
- Linked
- Related
- Hot Network Questions
- 1. Overview
- 2. int to byte array
- 2.1 BigInteger
- 2.2 DataOutputStream
- 2.3 ByteBuffer
- 2.4 Shift operations
- 3. byte array to int
- 3.1 ByteBuffer
- 3.2 Shift operations
- 4. int array to byte array
- 5. byte array to int array
- 5. Conclusion
- Related Posts
- Check between two datetime if passed in Java/Andro >
- java.io.FileNotFoundException: /u/applic/wsadmin/geronimo217/geronimo8/var/catalina/work/TomcatWebContainer/SESSIONS.ser (Permis.
- Remove space between images added into a single pdf file with iText using java.
- 13 Answers 13
- Not the answer you’re looking for? Browse other questions tagged java arrays integer byte or ask your own question.
- Linked
- Related
- Hot Network Questions
1. Overview
In this article, we will discuss various techniques of converting int to a byte array and vice versa, int array to byte array and so on.In java int data type take 4 bytes (32 bits) and it’s range is -2,147,483,648 to 2,147,483, 647.
2. int to byte array
Byte arrays are commonly used in applications that stream data byte-wise, such as socket connections that send data in byte arrays through TCP or UDP protocols.
It also used in applications that read/write binary files. Here we have discussed four different ways to convert int to byte array in java as below:
2.1 BigInteger
BigInteger provides support of all Java’s primitive integer operators and all methods from java.lang.Math.
BigInteger also provides operations for a prime generation, GCD calculation, modular arithmetic, primality testing, bit manipulation etc.. toByteArray method of BigInteger class is used to convert bigint to byte array.
First, we need to convert int to BigInteger using valueOf method of BigInterger and then use toByteArray method.
2.2 DataOutputStream
Second way to convert int to byte array is using ByteArrayOutputStream and DataOutputStream . ByteArrayOutputStream class provide one method toByteArray which return underlying written primitive types into a byte array format. Here we have write int to underlying stream and then use the toByteArray method to get equivalent byte array.
2.3 ByteBuffer
java.nio provide a facility which help us to work with buffers very efficiently. There are several buffer classes like ByteBuffer, IntBuffer, LongBuffer, MappedByteBuffer etc. under java.nio package. Here we have used ByteBuffer class to convert int to byte array using it’s array method.
2.4 Shift operations
We can convert int to byte array with the help of shift>> and & operators.
3. byte array to int
We can convert any byte array to int using below two techniques.
3.1 ByteBuffer
Java provides a ByteBuffer class to do the same.to convert any byte array, first we need to wrap up using ByteBuffer’s static method wrap and then by calling getInt() method we can get integer value of that byte array.
3.2 Shift operations
A second way to convert byte array to int is by using left shift operator and & operator. Left shift operator shifts the number to the left and fills remaining bits with zeros. It is similar to multiplying the number with the power of two.
4. int array to byte array
Now we can convert any int array to byte array using our earlier learning of int to byte array. In below example, we have iterate of all the elements from array, convert it to a byte array and prepare final byte array.
5. byte array to int array
Similarly when we converting byte array to int array, first we need to calculate the length of an array. Prepare four bytes chunk and convert it to integer and store it in an array.
5. Conclusion
In this article we have discussed various techniques of converting byte array to int, int to byte array, int array to byte array and byte array to int array using ByteBuffer,DataInputStream,DataOutputStream and some custom logic.
Posted by: admin November 13, 2017 Leave a comment
I got an integer: 1695609641
when I use method:
but I want a byte array:
How can I make this?
using Java NIO’s ByteBuffer is very simple:
The idea is not mine. I’ve taken it from some post on dzone.com.
The chunks below work at least for sending an int over UDP.
int to byte array:
byte array to int:
The class org.apache.hadoop.hbase.util.Bytes has a bunch of handy byte[] conversion methods, but you might not want to add the whole HBase jar to your project just for this purpose. It’s surprising that not only are such method missing AFAIK from the JDK, but also from obvious libs like commons io.
With it you can do this :
Full class is here : https://gist.github.com/superbob/6548493, it supports initialization from shorts or long
for the first byte
for the second and loop etc., writing into a preallocated byte array. A bit messy, unfortunately.
Related Posts
Check between two datetime if passed in Java/Andro >
Questions: I have two date time string, one is current time and second is given as follows. String currentTime = «05/30/2018 16:56:21»; String endTime = «05/30/2018 16:59:21»; Now I want to check if t.
java.io.FileNotFoundException: /u/applic/wsadmin/geronimo217/geronimo8/var/catalina/work/TomcatWebContainer/SESSIONS.ser (Permis.
Questions: I am facing ERROR [ManagerBase] IOException while saving persisted sessions: java.io.FileNotFoundException: /u/applic/wsadmin/geronimo217/geronimo8/var/catalina/work/TomcatWebContainer/SESS.
Remove space between images added into a single pdf file with iText using java.
Questions: I am trying to crate PDF files from a list of images. 4 image should cover a full page with no margin padding or anything. My problem is that the added images are separated by a white line.
I got an integer: 1695609641
when I use method:
but I want a byte array:
How can I make this?
13 Answers 13
using Java NIO’s ByteBuffer is very simple:
The idea is not mine. I’ve taken it from some post on dzone.com.
The chunks below work at least for sending an int over UDP.
int to byte array:
byte array to int:
Because generally you would want to convert this array back to an int at a later point, here are the methods to convert an array of ints into an array of bytes and vice-versa:
Note that because of sign propagation and such, the «& 0xFF. » are needed when converting back to the int.
for the first byte
for the second and loop etc., writing into a preallocated byte array. A bit messy, unfortunately.
The class org.apache.hadoop.hbase.util.Bytes has a bunch of handy byte[] conversion methods, but you might not want to add the whole HBase jar to your project just for this purpose. It’s surprising that not only are such method missing AFAIK from the JDK, but also from obvious libs like commons io.
With it you can do this :
Full class is here : https://gist.github.com/superbob/6548493, it supports initialization from shorts or long
Not the answer you’re looking for? Browse other questions tagged java arrays integer byte or ask your own question.
Linked
Related
Hot Network Questions
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2019 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0 with attribution required. rev 2019.11.15.35459
Источник: