Converts a base32 encoded string to a byte array - Java java.lang

Java examples for java.lang:byte Array Convert

Description

Converts a base32 encoded string to a byte array

Demo Code

/**//from  w ww  . j  a v a  2 s  . c o m
 * 
 * Copyright (C) 2004-2008 FhG Fokus
 *
 * This file is part of the FhG Fokus UPnP stack - an open source UPnP implementation
 * with some additional features
 *
 * You can redistribute the FhG Fokus UPnP stack and/or modify it
 * under the terms of the GNU General Public License Version 3 as published by
 * the Free Software Foundation.
 *
 * For a license to use the FhG Fokus UPnP stack software under conditions
 * other than those described here, or to purchase support for this
 * software, please contact Fraunhofer FOKUS by e-mail at the following
 * addresses:
 *   upnpstack@fokus.fraunhofer.de
 *
 * The FhG Fokus UPnP stack 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>
 * or write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */
//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String data = "java2s.com";
        System.out.println(java.util.Arrays
                .toString(base32ToByteArray(data)));
    }

    /** Converts a base32 encoded string to a byte array */
    public static byte[] base32ToByteArray(String data) {
        if ((data.length() + 1) % 10 != 0) {
            return null;
        }

        byte[] bits = new byte[(data.length() + 1) / 10 * 40];

        int bitIndex = 0;
        for (int i = 0; i < data.length(); i++) {
            char value = data.charAt(i);
            // ignore '-'
            if (value != '-') {
                byte byteValue = 0;
                if (value == '9') {
                    byteValue = 31;
                }
                if (value == '7') {
                    byteValue = 30;
                }
                if (value >= '2' && value <= '5') {
                    byteValue = (byte) (value - '2' + 26);
                }
                if (value >= 'A' && value <= 'Z') {
                    byteValue = (byte) (value - 'A');
                }
                // copy to bitArray
                bits[bitIndex] = (byte) ((byteValue & 0x10) >> 4);
                bits[bitIndex + 1] = (byte) ((byteValue & 0x08) >> 3);
                bits[bitIndex + 2] = (byte) ((byteValue & 0x04) >> 2);
                bits[bitIndex + 3] = (byte) ((byteValue & 0x02) >> 1);
                bits[bitIndex + 4] = (byte) ((byteValue & 0x01) >> 0);
                bitIndex += 5;
            }
        }
        // build byte array
        byte[] result = new byte[bits.length / 8];

        for (int i = 0; i < result.length; i++) {
            byte byteValue = (byte) (bits[i * 8 + 0] << 7
                    | bits[i * 8 + 1] << 6 | bits[i * 8 + 2] << 5
                    | bits[i * 8 + 3] << 4 | bits[i * 8 + 4] << 3
                    | bits[i * 8 + 5] << 2 | bits[i * 8 + 6] << 1 | bits[i * 8 + 7] << 0);

            result[i] = byteValue;
        }
        return result;
    }
}

Related Tutorials