Java Binary Encode toBinFromHex(final String hexSymbols)

Here you can find the source of toBinFromHex(final String hexSymbols)

Description

Transform a string of hex symbols to a string of binary symbols

License

Open Source License

Parameter

Parameter Description
hexSymbols The hex symbol string to transform

Return

The binary string that corresponds to input hex string

Declaration

public static String toBinFromHex(final String hexSymbols) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public final static int BITS_PER_HEX_DIGIT = 4;
    public static final String HEX_STRING_PREFIX1 = "0x";
    public static final String HEX_STRING_PREFIX2 = "0X";

    /**//from   w ww. java 2s.  co  m
     * Transform a string of hex symbols to a string of binary symbols
     *
     * @param hexSymbols
     *            The hex symbol string to transform
     * @return The binary string that corresponds to input hex string
     */
    public static String toBinFromHex(final String hexSymbols) {
        String hex = stripHexPrefix(hexSymbols);

        StringBuilder bits = new StringBuilder(hex.length() * BITS_PER_HEX_DIGIT);
        for (int i = 0; i < hex.length(); i++) {
            bits.append(toBinFromHexChar(hex.charAt(i)));
        }

        return (bits.toString());
    }

    public static String stripHexPrefix(String hexSymbols) {
        if (hexSymbols.startsWith(HEX_STRING_PREFIX1)) {
            hexSymbols = hexSymbols.substring(HEX_STRING_PREFIX1.length());
        } else if (hexSymbols.startsWith(HEX_STRING_PREFIX2)) {
            hexSymbols = hexSymbols.substring(HEX_STRING_PREFIX2.length());
        }

        return (hexSymbols);
    }

    public static String toBinFromHexChar(final char hex) {
        switch (hex) {
        case '0':
            return ("0000");
        case '1':
            return ("0001");
        case '2':
            return ("0010");
        case '3':
            return ("0011");
        case '4':
            return ("0100");
        case '5':
            return ("0101");
        case '6':
            return ("0110");
        case '7':
            return ("0111");
        case '8':
            return ("1000");
        case '9':
            return ("1001");
        case 'a':
        case 'A':
            return ("1010");
        case 'b':
        case 'B':
            return ("1011");
        case 'c':
        case 'C':
            return ("1100");
        case 'd':
        case 'D':
            return ("1101");
        case 'e':
        case 'E':
            return ("1110");
        case 'f':
        case 'F':
            return ("1111");
        default:
            throw new IllegalArgumentException(
                    "The input character \'" + hex + "\'is not a valid hexadecimal character.");
        }
    }
}

Related

  1. toBinaryString(long val)
  2. toBinaryString(long value, int bitLegnth)
  3. toBinaryStringAddress(long address)
  4. toBinaryText(StringBuffer buf)
  5. toBinFromByte(final byte b)
  6. toBinFromHexChar(final char hex)
  7. toBinFromOct(final String octSymbols)
  8. toBinFromOctChar(final char oct)
  9. toBinString(byte b)