Java Array Unpack unpack(byte[] array)

Here you can find the source of unpack(byte[] array)

Description

unpack

License

Open Source License

Declaration

public static StringBuffer unpack(byte[] array) 

Method Source Code

//package com.java2s;
/*-GNU-GPL-BEGIN-*//from w ww .jav  a2 s  . com
  This program 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 2 of the License, or
  (at your option) any later version.
    
  This program 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, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*-GNU-GPL-END-*/

public class Main {
    public static StringBuffer unpack(byte[] array) {

        int len = array.length;
        int length = len << 1;

        StringBuffer buf = new StringBuffer(length);
        buf.setLength(length);

        for (int i = 0, j = 0; i < len; ++i) {

            byte by = array[i];

            byte hi = (byte) ((by & 0xF0) >> 4);
            byte lo = (byte) (by & 0x0F);

            buf.setCharAt(j++, (char) hexaNibble(hi));
            buf.setCharAt(j++, (char) hexaNibble(lo));
        }

        return buf;
    }

    public static byte hexaNibble(byte by) {
        return (byte) ((by > 9) ? (by + 'a' - 10) : (by + '0'));
    }
}

Related

  1. unpack(byte[] bytes)
  2. unpack(double[] packed, int width, int height, int outputIndex)
  3. unpack(int[] sourcearray, int arraypos, int[] data, int datapos, int num, int b)
  4. unpackBCD(byte[] source)