Java Byte to Octal byteToOctalStr(byte b)

Here you can find the source of byteToOctalStr(byte b)

Description

byte To Octal Str

License

Open Source License

Parameter

Parameter Description
b a parameter

Declaration

public static String byteToOctalStr(byte b) 

Method Source Code

//package com.java2s;
/*/*from w w  w . j  av a2  s .  com*/
 * This file is part of trolCommander, http://www.trolsoft.ru/soft/trolcommander
 * Copyright (C) 2013-2014 Oleg Trifonov
 *
 * muCommander is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * muCommander 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/>.
 */

public class Main {
    private static final String[] STRING_OF_ZERO = { "", "0", "00", "000", "0000", "00000", "000000", "0000000",
            "00000000", "000000000", "0000000000" };
    private static final String[] OCTAL_BYTE_STRINGS = new String[256];

    /**
     *
     * @param b
     * @return
     */
    public static String byteToOctalStr(byte b) {
        int v = b & 0xFF;
        return byteToOctalStr(v);
    }

    /**
     *
     * @param v
     * @return
     */
    public static String byteToOctalStr(int v) {
        String result = OCTAL_BYTE_STRINGS[v];
        if (result == null) {
            result = Integer.toOctalString(v);
            result = STRING_OF_ZERO[3 - result.length()] + result;
            OCTAL_BYTE_STRINGS[v] = result;
        }
        return result;
    }
}