Java Hex Calculate toHexaString(byte[] data)

Here you can find the source of toHexaString(byte[] data)

Description

to Hexa String

License

Open Source License

Declaration

public static String toHexaString(byte[] data) 

Method Source Code

//package com.java2s;
/* //from  w  w w  . ja va  2 s . co  m
 * Copyright 2012 Devoteam http://www.devoteam.com
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * 
 * 
 * This file is part of Multi-Protocol Test Suite (MTS).
 * 
 * Multi-Protocol Test Suite (MTS) 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 3 of the
 * License.
 * 
 * Multi-Protocol Test Suite (MTS) 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 Multi-Protocol Test Suite (MTS).
 * If not, see <http://www.gnu.org/licenses/>.
 * 
 */

public class Main {
    public static String toHexaString(byte[] data) {
        return toHexaString(data, 0, -1);
    }

    public static String toHexaString(byte[] data, String sep) {
        return toHexaString(data, 0, -1, sep);
    }

    public static String toHexaString(byte[] data, int offset, int length) {
        return toHexaString(data, offset, length, ".");
    }

    public static String toHexaString(byte[] data, int offset, int length, String sep) {
        StringBuffer buffer = new StringBuffer();
        if (length == -1) {
            length = data.length - offset;
        }

        for (int i = offset; i < offset + length; i++) {
            int value = (data[i] & 0xff) + 0x100;
            buffer.append(Integer.toString(value, 16).substring(1));
            if (sep != null) {
                buffer.append(sep);
            }
        }
        String res = buffer.toString();
        return res;
    }
}

Related

  1. toHexadecimal(final byte[] array)
  2. toHexadecimal(final byte[] in)
  3. toHexadecimal(final byte[] message)
  4. toHexadecimealString(byte[] data, int length, boolean uppercase)
  5. toHexArray(byte[] binary)
  6. toHexaString(int value)
  7. toHexByte(byte b)
  8. toHexByte(byte b, StringBuffer sb)
  9. toHexByte(final char ch1, final char ch2)