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, ∼2730🔥, 0💬
Popular Posts:
jTDS JDBC Driver Source Code Files are provided in the source package file, jtds-1.3.1-fyi.zip. You ...
JBrowser Source Code Files are provided in the source package file. You can download JBrowser source...
How to show the XML parsing flow with sax\DocumentTracer.java provided in the Apache Xerces package?...
What Is wstx-asl-3.2.8.jar? wstx-asl-3.2.8.jar is JAR file for the ASL component of Woodstox 3.2.8. ...
JDK 11 java.rmi.jmod is the JMOD file for JDK 11 RMI (Remote Method Invocation) module. JDK 11 RMI m...