Here you can find the source of toAsciiChars(byte[] raw)
Parameter | Description |
---|---|
raw | the raw binary data to convert |
public static char[] toAsciiChars(byte[] raw)
/*//from w w w . j a v a 2s . co m * Copyright 2001-2004 The Apache Software Foundation. * * 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. */ import org.apache.commons.codec.BinaryDecoder; import org.apache.commons.codec.BinaryEncoder; import org.apache.commons.codec.DecoderException; import org.apache.commons.codec.EncoderException; public class Main{ /** Empty char array. */ private static final char[] EMPTY_CHAR_ARRAY = new char[0]; private static final int[] BITS = { BIT_0, BIT_1, BIT_2, BIT_3, BIT_4, BIT_5, BIT_6, BIT_7 }; /** * Converts an array of raw binary data into an array of ascii 0 and 1 characters. * * @param raw * the raw binary data to convert * @return an array of 0 and 1 characters for each bit of the argument * @see org.apache.commons.codec.BinaryEncoder#encode(byte[]) */ public static char[] toAsciiChars(byte[] raw) { if (raw == null || raw.length == 0) { return EMPTY_CHAR_ARRAY; } // get 8 times the bytes with 3 bit shifts to the left of the length char[] l_ascii = new char[raw.length << 3]; /* * We decr index jj by 8 as we go along to not recompute indices using multiplication every time inside the * loop. */ for (int ii = 0, jj = l_ascii.length - 1; ii < raw.length; ii++, jj -= 8) { for (int bits = 0; bits < BITS.length; ++bits) { if ((raw[ii] & BITS[bits]) == 0) { l_ascii[jj - bits] = '0'; } else { l_ascii[jj - bits] = '1'; } } } return l_ascii; } }