Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (101)
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 (309)
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, 1924🔥, 0💬
Popular Posts:
maven-core-3.8.6.jar is the JAR file for Apache Maven 3.8.6 Core module. Apache Maven is a software ...
What Is activation.jar? I heard it's related to JAF (JavaBeans Activation Framework) 1.1? The if you...
How to download and install ojdbc14.jar for Oracle 10g R2? ojdbc14.jar for Oracle 10g R2 is a Java 1...
Jackson is "the Java JSON library" or "the best JSON parser for Java". Or simply as "JSON for Java"....
JDK 11 java.rmi.jmod is the JMOD file for JDK 11 RMI (Remote Method Invocation) module. JDK 11 RMI m...