Get binary length of string - Java java.lang

Java examples for java.lang:String Length

Description

Get binary length of string

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String inputString = "10101010101010101";
        System.out.println(bin2int(inputString));
    }//from  w w w.  j a  v a  2  s  .c  o  m

    /**
     * Get binary length of string
     * @param inputString
     * @return
     */
    public static int bin2int(String inputString) {
        int index;
        int tempLength;
        int returnLength = 0;
        byte[] tempArray;

        if (inputString == null)
            return returnLength;

        tempArray = inputString.getBytes();
        tempLength = tempArray.length;
        for (index = 0; index < tempLength; returnLength = (returnLength * 2)
                + tempArray[index] - '0', index++)
            ;

        return returnLength;
    }
}

Related Tutorials