Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright 2016 Hippo Seven
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    private static final int COUNT_NUMBER = '9' - '0' + 1;
    private static final int COUNT_UPPERCASE_LETTER = 'z' - 'a' + 1;
    private static final int COUNT_LOWERCASE_LETTER = 'Z' - 'A' + 1;
    private static final int COUNT_SUM = COUNT_NUMBER + COUNT_UPPERCASE_LETTER + COUNT_LOWERCASE_LETTER;

    private static String toReadableString(byte[] bytes) {
        StringBuilder sb = new StringBuilder(bytes.length);
        for (byte b : bytes) {
            sb.append(getReadableChar(b));
        }
        return sb.toString();
    }

    private static char getReadableChar(byte b) {
        int i = (b & 0xFF) % COUNT_SUM;
        if (i < COUNT_NUMBER) {
            return (char) ('0' + i);
        } else if (i < COUNT_NUMBER + COUNT_UPPERCASE_LETTER) {
            return (char) ('a' + i - COUNT_NUMBER);
        } else if (i < COUNT_NUMBER + COUNT_UPPERCASE_LETTER + COUNT_LOWERCASE_LETTER) {
            return (char) ('A' + i - COUNT_NUMBER - COUNT_UPPERCASE_LETTER);
        } else {
            return '?';
        }
    }
}