Converts a byte array to Base32 encoding. - Java java.lang

Java examples for java.lang:String Base32

Description

Converts a byte array to Base32 encoding.

Demo Code

/**/* w  ww  .j  a v  a 2s .  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) {
        byte[] data = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(byteArrayToBase32(data));
    }

    /**
     * Converts a byte array to Base32 encoding. data.length mod 5 must be 0.
     */
    public static String byteArrayToBase32(byte[] data) {
        String result = "";
        if (data.length % 5 != 0) {
            return result;
        }

        byte[] bits = new byte[data.length * 8];
        // convert to bits
        for (int i = 0; i < data.length; i++) {
            bits[i * 8] = (byte) ((data[i] & 0x80) >> 7);
            bits[i * 8 + 1] = (byte) ((data[i] & 0x40) >> 6);
            bits[i * 8 + 2] = (byte) ((data[i] & 0x20) >> 5);
            bits[i * 8 + 3] = (byte) ((data[i] & 0x10) >> 4);
            bits[i * 8 + 4] = (byte) ((data[i] & 0x08) >> 3);
            bits[i * 8 + 5] = (byte) ((data[i] & 0x04) >> 2);
            bits[i * 8 + 6] = (byte) ((data[i] & 0x02) >> 1);
            bits[i * 8 + 7] = (byte) ((data[i] & 0x01) >> 0);
        }
        // extract 5 bit values and convert to string
        for (int i = 0; i < data.length / 5 * 8; i++) {
            if (i > 0 && i % 4 == 0) {
                result += '-';
            }
            byte value = (byte) (bits[i * 5 + 0] << 4
                    | bits[i * 5 + 1] << 3 | bits[i * 5 + 2] << 2
                    | bits[i * 5 + 3] << 1 | bits[i * 5 + 4] << 0);

            if (value >= 0 && value < 26) {
                result = result + (char) (value + 'A');
            }

            if (value >= 26 && value < 30) {
                result = result + (char) (value - 26 + '2');
            }

            if (value == 30) {
                result = result + '7';
            }

            if (value == 31) {
                result = result + '9';
            }
        }
        return result;
    }
}

Related Tutorials