This function decodes a base 32 string into its corresponding byte array. - Java java.lang

Java examples for java.lang:String Base64

Description

This function decodes a base 32 string into its corresponding byte array.

Demo Code

/************************************************************************
 * Copyright (c) Crater Dog Technologies(TM).  All Rights Reserved.     *
 ************************************************************************
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.        *
 *                                                                      *
 * This code is free software; you can redistribute it and/or modify it *
 * under the terms of The MIT License (MIT), as published by the Open   *
 * Source Initiative. (See http://opensource.org/licenses/MIT)          *
 ************************************************************************/
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String base32 = "java2s.com";
        System.out.println(java.util.Arrays.toString(decode(base32)));
    }/*from w w  w.j a v  a2s  .co m*/

    static private final String lookupTable = "0123456789ABCDFGHJKLMNPQRSTVWXYZ";

    /**
     * This function decodes a base 32 string into its corresponding byte array.
     *
     * @param base32 The base 32 encoded string.
     * @return The corresponding byte array.
     */
    static public byte[] decode(String base32) {
        String string = base32.replaceAll("\\s", ""); // remove all white space
        int length = string.length();
        byte[] bytes = new byte[length * 5 / 8];
        for (int i = 0; i < length - 1; i++) {
            char character = string.charAt(i);
            byte chunk = (byte) lookupTable.indexOf((int) character);
            if (chunk < 0)
                throw new NumberFormatException(
                        "Attempted to decode a string that is not base 32: "
                                + string);
            decodeCharacter(chunk, i, bytes, 0);
        }
        if (length > 0) {
            char character = string.charAt(length - 1);
            byte chunk = (byte) lookupTable.indexOf((int) character);
            if (chunk < 0)
                throw new NumberFormatException(
                        "Attempted to decode a string that is not base 32: "
                                + string);
            decodeLastCharacter(chunk, length - 1, bytes, 0);
        }
        return bytes;
    }

    static private void decodeCharacter(byte chunk, int characterIndex,
            byte[] bytes, int index) {
        int byteIndex = index + (characterIndex * 5) / 8;
        int offset = characterIndex % 8;
        switch (offset) {
        case 0:
            bytes[byteIndex] |= chunk << 3;
            break;
        case 1:
            bytes[byteIndex] |= chunk >>> 2;
            bytes[byteIndex + 1] |= chunk << 6;
            break;
        case 2:
            bytes[byteIndex] |= chunk << 1;
            break;
        case 3:
            bytes[byteIndex] |= chunk >>> 4;
            bytes[byteIndex + 1] |= chunk << 4;
            break;
        case 4:
            bytes[byteIndex] |= chunk >>> 1;
            bytes[byteIndex + 1] |= chunk << 7;
            break;
        case 5:
            bytes[byteIndex] |= chunk << 2;
            break;
        case 6:
            bytes[byteIndex] |= chunk >>> 3;
            bytes[byteIndex + 1] |= chunk << 5;
            break;
        case 7:
            bytes[byteIndex] |= chunk;
            break;
        }
    }

    static private void decodeLastCharacter(byte chunk, int characterIndex,
            byte[] bytes, int index) {
        int byteIndex = index + (characterIndex * 5) / 8;
        int offset = characterIndex % 8;
        switch (offset) {
        case 1:
            bytes[byteIndex] |= chunk >>> 2;
            break;
        case 3:
            bytes[byteIndex] |= chunk >>> 4;
            break;
        case 4:
            bytes[byteIndex] |= chunk >>> 1;
            break;
        case 6:
            bytes[byteIndex] |= chunk >>> 3;
            break;
        case 7:
            bytes[byteIndex] |= chunk;
            break;
        }
    }
}

Related Tutorials