Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (102)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (322)
Collections:
Other Resources:
org.apache.commons.codec.binary.Hex Example
What is org.apache.commons.codec.binary.Hex class? How to use org.apache.commons.codec.binary.Hex class?
✍: FYIcenter.com
org.apache.commons.codec.binary.Hex class is a Java class
offered in commons-codec.jar that
converts between byte arrays and strings hexadecimal digits.
Two commonly used static methods in org.apache.commons.codec.binary.Hex class are:
1. The encoding method:
public static char[] encodeHex(byte[] data)
Converts an array of bytes into an array of characters representing
the hexadecimal values of each byte in order.
The returned array will be double the length of the passed array,
as it takes two characters to represent any given byte.
Parameters:
data - a byte[] to convert to Hex characters
Returns:
A char[] containing hexadecimal characters
2. The decoding method:
public static byte[] decodeHex(char[] data) throws DecoderException
Converts an array of characters representing hexadecimal values into
an array of bytes of those same values. The returned array will be half
the length of the passed array, as it takes two characters to represent
any given byte. An exception is thrown if the passed char array has
an odd number of elements.
Parameters:
data - An array of characters containing hexadecimal digits
Returns:
A byte array containing binary data decoded from the supplied char array.
Here is a simple example of org.apache.commons.codec.binary.Hex class:
// Copyright (c) 2016 FYIcenter.com
import org.apache.commons.codec.binary.Hex;
// Example of using the Hex class
public class HexExample {
public static void main(String[] args) throws Exception {
System.out.println("encodeHex() Example:");
String inputString = "ABCD1234";
byte[] inputBytes = inputString.getBytes("UTF8");
char[] outputChars = Hex.encodeHex(inputBytes);
String outputString = new String(outputChars);
String expectedString = "4142434431323334";
System.out.println(" Input string: "+inputString);
System.out.println(" Encoded string: "+outputString);
System.out.println(" Expected string: "+expectedString);
System.out.println("decodeHex() Example:");
inputString = "4142434431323334";
char[] inputChars = inputString.toCharArray();
byte[] outputBytes = Hex.decodeHex(inputChars);
outputString = new String(outputBytes,"UTF8");
expectedString = "ABCD1234";
System.out.println(" Input string: "+inputString);
System.out.println(" Encoded string: "+outputString);
System.out.println(" Expected string: "+expectedString);
}
}
You can compile and run the above example in a command window as shown below:
C:\fyicenter>\local\jdk-1.8.0\bin\javac -cp C:\local\commons-codec-1.10\commons-codec-1.10.jar HexExample.java C:\fyicenter>\local\jdk-1.8.0\bin\java -cp .;C:\local\commons-codec-1.10\commons-codec-1.10.jar HexExample encodeHex() Example: Input string: ABCD1234 Encoded string: 4142434431323334 Expected string: 4142434431323334 decodeHex() Example: Input string: 4142434431323334 Encoded string: ABCD1234 Expected string: ABCD1234
⇒ org.apache.commons.codec.digest.DigestUtils Example
⇐ org.apache.commons.codec.binary.BinaryCodec Example
2017-04-22, ∼3519🔥, 0💬
Popular Posts:
JLayer is a library that decodes/plays/converts MPEG 1/2/2.5 Layer 1/2/3 (i.e. MP3) in real time for...
JDK 11 jdk.xml.dom.jmod is the JMOD file for JDK 11 XML DOM module. JDK 11 XML DOM module compiled c...
What Is poi-scratchpad-5.2.3.jar ?poi-scratchpad-5.2.3.jar is one of the JAR files for Apache POI 5....
JDK 11 java.sql.rowset.jmod is the JMOD file for JDK 11 SQL Rowset module. JDK 11 SQL Rowset module ...
Where Can I see Java Source Code files for Xerces Java 2.11.2? Here are Java Source Code files for X...