Here you can find the source of toAsciiBytes(byte[] raw)
Parameter | Description |
---|---|
raw | the raw binary data to convert |
public static byte[] toAsciiBytes(byte[] raw)
/*/*from w w w . ja v a 2 s . c o 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 byte array. */ private static final byte[] EMPTY_BYTE_ARRAY = new byte[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 character bytes - each byte is a truncated * char. * * @param raw * the raw binary data to convert * @return an array of 0 and 1 character bytes for each bit of the argument * @see org.apache.commons.codec.BinaryEncoder#encode(byte[]) */ public static byte[] toAsciiBytes(byte[] raw) { if (raw == null || raw.length == 0) { return EMPTY_BYTE_ARRAY; } // get 8 times the bytes with 3 bit shifts to the left of the length byte[] l_ascii = new byte[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; } }