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.BinaryCodec Example
What is org.apache.commons.codec.binary.BinaryCodec class? How to use org.apache.commons.codec.binary.BinaryCodec class?
✍: FYIcenter.com
org.apache.commons.codec.binary.BinaryCodec class is a Java class
offered in commons-codec.jar that
converts between byte arrays and strings of "0"s and "1"s.
Two commonly used instance methods in org.apache.commons.codec.binary.BinaryCodec class are:
1. The encoding method:
public byte[] encode(byte[] raw)
Converts an array of raw binary data into an array of ASCII 0 and 1 characters.
The last byte from the input is converted into 8 bytes first.
Parameters:
raw - the raw binary data to convert
Returns:
0 and 1 ASCII character bytes, one for each bit of the argument.
2. The decoding method:
public byte[] decode(byte[] ascii)
Decodes a byte array where each byte represents an ASCII '0' or '1'.
The last 8 bytes from the input is converted into 1 byte first.
Parameters:
ascii - each byte represents an ASCII '0' or '1'
Returns:
the raw encoded binary where each bit corresponds to a byte in the byte array argument
Here is a simple example of org.apache.commons.codec.binary.Base64 class:
// Copyright (c) 2016 FYIcenter.com
import org.apache.commons.codec.binary.BinaryCodec;
// Example of using the BinaryCodec class
public class BinaryCodecExample {
public static void main(String[] args) throws Exception {
BinaryCodec codec = new BinaryCodec();
System.out.println("BinaryCodec encode() Example:");
byte[] inputBytes = new byte[4];
inputBytes[0] = (byte) 0xff;
inputBytes[1] = (byte) 0xf0;
inputBytes[2] = (byte) 0x0f;
inputBytes[3] = (byte) 0x00;
byte[] outputBytes = codec.encode(inputBytes);
String outputString = new String(outputBytes,"UTF8");
String expectedString = "00000000000011111111000011111111";
System.out.println(" Input bytes: "+(inputBytes[0]&0xff)
+", "+(inputBytes[1]&0xff)
+", "+(inputBytes[2]&0xff)
+", "+(inputBytes[3]&0xff));
System.out.println(" Encoded string: "+outputString);
System.out.println(" Expected string: "+expectedString);
System.out.println("BinaryCodec decode() Example:");
String inputString = "00000000000011111111000011111111";
inputBytes = inputString.getBytes("UTF8");
outputBytes = codec.decode(inputBytes);
System.out.println(" Input string: "+inputString);
System.out.println(" Encoded bytes: "+(outputBytes[0]&0xff)
+", "+(outputBytes[1]&0xff)
+", "+(outputBytes[2]&0xff)
+", "+(outputBytes[3]&0xff));
}
}
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 BinaryCodecExample.java C:\fyicenter>\local\jdk-1.8.0\bin\java -cp .;C:\local\commons-codec-1.10\commons-codec-1.10.jar BinaryCodecExample BinaryCodec encode() Example: Input bytes: 255, 240, 15, 0 Encoded string: 00000000000011111111000011111111 Expected string: 00000000000011111111000011111111 BinaryCodec decode() Example: Input string: 00000000000011111111000011111111 Encoded bytes: 255, 240, 15, 0
⇒ org.apache.commons.codec.binary.Hex Example
⇐ org.apache.commons.codec.binary.Base64 Example
2017-04-08, ∼2824🔥, 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.jfr.jmod is the JMOD file for JDK 11 JFR module. JDK 11 JFR module compiled class files a...
JAX-WS is an API for building web services and clients. It is the next generation Web Services API r...
JDK 17 jdk.dynalink.jmod is the JMOD file for JDK 17 Dynamic Linking module. JDK 17 Dynamic Linking ...
How to download and install ojdbc14.jar for Oracle 10g R2? ojdbc14.jar for Oracle 10g R2 is a Java 1...