Converts a string into a BitSet. - Java java.util

Java examples for java.util:BitSet

Description

Converts a string into a BitSet.

Demo Code


//package com.java2s;

import java.util.BitSet;

public class Main {
    public static void main(String[] argv) throws Exception {
        String sbits = "java2s.com";
        System.out.println(stringToBitSet(sbits));
    }/*from  ww w  .  ja v a  2s  . co m*/

    /**
     * Converts a string into a BitSet. 
     * The first character is the most significant bit. 
     * a '1' character is converted to true. 
     * Every other character is converted to false.
     * 
     * @param sbits The string to convert.
     * @return BitSet representing the binary value.
     */
    public static BitSet stringToBitSet(String sbits) {
        BitSet bits = new BitSet(sbits.length());

        for (int i = sbits.length() - 1; i >= 0; i--) {
            bits.set(sbits.length() - 1 - i, (sbits.charAt(i) == '1'));
        }

        return bits;
    }
}

Related Tutorials