Android Hex String Print toFormattedHexString(StringBuffer text, byte[] data)

Here you can find the source of toFormattedHexString(StringBuffer text, byte[] data)

Description

to Formatted Hex String

License

Open Source License

Declaration

public static StringBuffer toFormattedHexString(StringBuffer text,
            byte[] data) 

Method Source Code

//package com.java2s;
/** //w  w w. j  av a2s  .c o  m
 * This reference code is an implementation of the IEEE P2200 standard.  It is not
 * a contribution to the IEEE P2200 standard.
 * 
 * Copyright (c) 2011 SanDisk Corporation.  All rights reserved.
 * 
 * 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 char[] sHex = { '0', '1', '2', '3', '4', '5', '6',
            '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 0 };
    private static final String sSep = System.getProperty("line.separator") == null
            || System.getProperty("line.separator") == "" ? "\n" : System
            .getProperty("line.separator");

    public static String toFormattedHexString(byte[] data) {
        return data == null ? "" : toFormattedHexString(
                new StringBuffer(data.length << 2), data).toString();
    }

    public static StringBuffer toFormattedHexString(StringBuffer text,
            byte[] data) {
        if (data != null)
            for (int index = 0; index < data.length; index++) {
                if (index > 0)
                    if (index % 8 == 0)
                        if (index % 32 == 0)
                            text.append(sSep);
                        else
                            text.append("  ");
                    else
                        text.append(' ');

                text.append(sHex[((int) data[index] >>> 4) & 0x0f]).append(
                        sHex[(int) data[index] & 0x0f]);
            }
        return text;
    }
}

Related

  1. printHex(byte[] array, int offset, int len)
  2. toFormattedHexString(byte[] data)