Java Hex Calculate toHexString(byte[] data)

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

Description

to Hex String

License

Open Source License

Declaration

public static String toHexString(byte[] data) 

Method Source Code

//package com.java2s;
/*/*from   w  w  w .  j  a va 2 s  .com*/
 * Water Quality Monitor Java Basestation
 * Copyright (C) 2013  nigelb
 *
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

public class Main {
    public static String toHexString(byte[] data) {
        return toHexString(toIntArray(data));
    }

    public static String toHexString(int[] data) {
        StringBuilder buf = new StringBuilder();
        buf.append("[");
        String del = "";
        for (int i : data) {
            String s = Integer.toHexString(i);

            buf.append(del).append("0x");
            if (s.length() == 1) {
                buf.append("0");
            }
            buf.append(s);
            del = ", ";
        }
        buf.append("]");
        return buf.toString();
    }

    public static int[] toIntArray(byte[] data) {
        int[] dat = new int[data.length];
        for (int i = 0; i < data.length; i++) {
            dat[i] = (data[i] & 0xff);
        }
        return dat;
    }
}

Related

  1. toHexString(byte[] bytes, int offset, int len, int max)
  2. toHexString(byte[] bytes, String separator, boolean upperCase)
  3. toHexString(byte[] coded)
  4. toHexString(byte[] color)
  5. toHexString(byte[] content, int len)
  6. toHexString(byte[] data)
  7. toHexString(byte[] data)
  8. toHexString(byte[] data)
  9. toHexString(byte[] data)