Java BitSet hexToBits(String s, BitSet ba, int length)

Here you can find the source of hexToBits(String s, BitSet ba, int length)

Description

Read a hex string of bits and write it into a bitset

License

Open Source License

Parameter

Parameter Description
s hex string of the stored bits
ba the bitset to store the bits in
length the maximum number of bits to store

Declaration

public static void hexToBits(String s, BitSet ba, int length) 

Method Source Code


//package com.java2s;
/*/*  w  ww  .j  ava  2 s. c  o  m*/
 * JGrass - Free Open Source Java GIS http://www.jgrass.org 
 * (C) HydroloGIS - www.hydrologis.com 
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Library General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option) any
 * later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Library General Public License
 * along with this library; if not, write to the Free Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import java.util.BitSet;

public class Main {
    /**
     * Read a hex string of bits and write it into a bitset
     * @param s hex string of the stored bits
     * @param ba the bitset to store the bits in
     * @param length the maximum number of bits to store 
     */
    public static void hexToBits(String s, BitSet ba, int length) {
        byte[] b = hexToBytes(s);
        bytesToBits(b, ba, length);
    }

    public static final byte[] hexToBytes(String s) {
        return hexToBytes(s, 0);
    }

    public static final byte[] hexToBytes(String s, int off) {
        byte[] bs = new byte[off + (1 + s.length()) / 2];
        hexToBytes(s, bs, off);
        return bs;
    }

    /**
     * Converts a String of hex characters into an array of bytes.
     * 
     * @param s
     *            A string of hex characters (upper case or lower) of even
     *            length.
     * @param out
     *            A byte array of length at least s.length()/2 + off
     * @param off
     *            The first byte to write of the array
     */
    public static final void hexToBytes(String s, byte[] out, int off)
            throws NumberFormatException, IndexOutOfBoundsException {
        int slen = s.length();
        if ((slen % 2) != 0) {
            s = '0' + s;
        }

        if (out.length < off + slen / 2) {
            throw new IndexOutOfBoundsException(
                    "Output buffer too small for input (" + out.length + "<" + off + slen / 2 + ")");
        }

        // Safe to assume the string is even length
        byte b1, b2;
        for (int i = 0; i < slen; i += 2) {
            b1 = (byte) Character.digit(s.charAt(i), 16);
            b2 = (byte) Character.digit(s.charAt(i + 1), 16);
            if (b1 < 0 || b2 < 0) {
                throw new NumberFormatException();
            }
            out[off + i / 2] = (byte) (b1 << 4 | b2);
        }
    }

    /**
     * Read bits from a byte array into a bitset
     * @param b the byte[] to read from
     * @param ba the bitset to write to
     */
    public static void bytesToBits(byte[] b, BitSet ba, int maxSize) {
        int x = 0;
        for (int i = 0; i < b.length; i++) {
            for (int j = 0; j < 8; j++) {
                if (x > maxSize)
                    break;
                int mask = 1 << j;
                boolean value = (mask & b[i]) != 0;
                ba.set(x, value);
                x++;
            }
        }
    }
}

Related

  1. getLemma(String[] tokens, BitSet bits, String delimiter)
  2. getSetBitIndices(BitSet bitSet)
  3. hammingDistance(BitSet bs1, BitSet bs2)
  4. hasAtLeastOneBitSet(BitSet bitSet, Iterable indexes)
  5. hex2BitSet(byte[] b, int offset, int maxBits)
  6. increment(BitSet bits, int size)
  7. insertBits(BitSet fromSet, int insertBefore, int count)
  8. intArrToBitSet(ArrayList integerArray)
  9. intToBitSet(int value)