Use the classes found in the java.nio
namespace, in particular, the ByteBuffer
. It can do all the work for you.
byte[] arr = { 0x00, 0x01 }; ByteBuffer wrapped = ByteBuffer.wrap(arr); // big-endian by default short num = wrapped.getShort(); // 1 ByteBuffer dbuf = ByteBuffer.allocate(2); dbuf.putShort(num); byte[] bytes = dbuf.array(); // { 0, 1 }
Related Posts:
- Which is the difference between Long.valueOf(0) and 0L in Java?
- Double precision floating values in Python?
- Double precision floating values in Python?
- Long vs Integer, long vs int, what to use and when?
- Pointtype command for gnuplot
- Check if a value is an object in JavaScript
- If Java is Strongly typed then why does this code compile?
- Node cannot be resolved to a type
- Possible causes of Java VM EXCEPTION_ACCESS_VIOLATION?
- java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Eclipse
- “Char cannot be dereferenced” error
- java.net.ConnectException: Connection refused
- What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
- Exception in thread “main” java.lang.Error: Unresolved compilation problems
- Difference between Inheritance and Composition
- What is the reason behind “non-static method cannot be referenced from a static context”? [duplicate]
- What’s the C++ version of Java’s ArrayList
- Java Could not reserve enough space for object heap error
- Getting “java.io.IOException: An existing connection was forcibly closed by the remote host”
- Java string to date conversion
- Exception in Application start method java.lang.reflect.InvocationTargetException
- Non-static variable cannot be referenced from a static context
- How to get an enum value from a string value in Java
- Round a double to 2 decimal places [duplicate]
- Int division: Why is the result of 1/3 == 0?
- Given final block not properly padded
- What is Parse/parsing?
- What is IllegalStateException?
- Java Using Nodes with LinkedList
- Why does cache use Most Recently Used (MRU) algorithm as evict policy?
- Difference between HashMap and Map in Java..?
- javac not working in windows command prompt
- Java returns error “Cannot instantiate the type”
- How does a Breadth-First Search work when looking for Shortest Path?
- Java: using switch statement with enum under subclass
- Java way to check if a string is palindrome
- No String-argument constructor/factory method to deserialize from String value (”)
- Where can i get BigClip? [closed]
- What is the /= operator in Java?
- ow to create a histogram in java
- java.io.IOException: Broken pipe
- How to execute a https GET request from java
- How to use VisibleForTesting for pure JUnit tests
- What is the easiest/best/most correct way to iterate through the characters of a string in Java?
- java.util.NoSuchElementException: No line found
- How can I list the available Cipher algorithms?
- Difference between numeric, float and decimal in SQL Server
- ADK vs JDK vs SDK difference?
- round up to 2 decimal places in java?
- Missing method body, or declare abstract in Java
- java code is showing error. ( ‘;’,expected) [closed]
- How can I edit a .jar file?
- java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver Exception occurring. Why?
- JAVA_HOME should point to a JDK not a JRE
- Converting a string to an integer on Android
- Error: Generic Array Creation
- How to center the text in a JLabel?
- What is the use of a private static variable in Java?
- Call a Class From another class
- Installing WindowBuilder on Eclipse Neon
- Java: Error creating a GZIPInputStream: Not in GZIP format
- Java: how to initialize String[]?
- Regular Expressions on Punctuation
- How to make a countdown timer in Java
- How to set String’s font size, style in Java using the Font class?
- (Java) Tic-Tac-Toe game using 2 dimensional Array
- Java syntax explanation – getMenuInflater()
- String was not recognized as a valid DateTime ” format dd/MM/yyyy”
- How can I remove a substring from a given String?
- Java get String CompareTo as a comparator object
- What does super.paintComponent(g) do?
- Union or intersection of Java Sets
- Get an OutputStream into a String
- Difference between string object and string literal
- Best way to create enum of strings?
- Why use getters and setters/accessors?
- How to use a Do-while loop that continuously prompts a user?
- Java equivalent of unsigned long long?
- Getting a “no interface expected here” when I try to subclass ServiceConnection [duplicate]
- How to determine day of week by passing specific date?
- Python-like list comprehension in Java
- Can an int be null in Java?
- What’s the difference between HashSet and Set?
- Netbeans – Error: Could not find or load main class
- Eclipse IDE – Error: Build path specifies execution environment Java SE 1.7
- Why int[] a = new int[1] instead of just int a?
- ArithmeticException: “Non-terminating decimal expansion; no exact representable decimal result”
- What’s the console.log() of java?
- When to use a Map instead of a List in Java?
- How do I replace a character in a string in Java?
- What’s so special about 0x7f?
- Append a single character to a string or char array in java?
- Preventing a Java class from being instantiated and inherited
- Collision Detection between two images in Java
- What does .pack() do?
- String is immutable. What exactly is the meaning?
- How do I autoindent in Netbeans?
- Class JavaLaunchHelper is implemented in two places
- What’s the equivalent of Java’s Thread.sleep() in JavaScript?
- No Main class found in NetBeans