Java Byte Array to Hex byteToHex(byte[] buf)

Here you can find the source of byteToHex(byte[] buf)

Description

converting a byte[] to it's hexadecimal format String

License

Open Source License

Parameter

Parameter Description
buf the byte to convert

Return

the hex String corresponding to buf

Declaration

public static String byteToHex(byte[] buf) 

Method Source Code

//package com.java2s;
/*/*from   w  ww  .ja v  a2  s.com*/
 * Copyright (C) 2010 - 2012 Jenia Software.
 *
 * This file is part of Sinekarta
 *
 * Sinekarta 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, or
 * (at your option) any later version.
 *
 * Sinekarta 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.
 *
 */

public class Main {
    /**
     * converting a byte[] to it's hexadecimal format String
     * 
     * @param buf the byte to convert
     * @return the hex String corresponding to buf
     */
    public static String byteToHex(byte[] buf) {
        if (buf == null)
            return null;
        return byteToHex(buf, 0, buf.length);
    }

    /**
     * converting a byte[] to it's hexadecimal format String
     * 
     * @param buf the byte to convert
     * @offset the offset to star with conversion
     * @len how many byte to convert
     * @return the hex String corresponding to buf
     */
    public static String byteToHex(byte[] buf, int offset, int len) {
        if (buf == null)
            return null;
        StringBuffer ret = new StringBuffer();
        long tmpL = 0;
        String tmp;
        for (int i = 0; i < len / 8; i++) {
            for (int k = 0; k < 8; k++) {
                tmpL = tmpL << 8;
                tmpL = tmpL | (0xff & buf[(i * 8) + k + offset]);
            }
            tmp = Long.toHexString(tmpL);
            for (int j = 0; j < 16 - tmp.length(); j++) {
                ret.append('0');
            }
            ret.append(tmp);
            tmpL = 0;
        }
        int mod = len % 8;
        if (mod != 0) {
            for (int k = len - mod; k < len; k++) {
                tmpL = tmpL << 8;
                tmpL = tmpL | (0xff & buf[k + offset]);
            }
            tmp = Long.toHexString(tmpL);
            for (int j = 0; j < (mod * 2) - tmp.length(); j++) {
                ret.append('0');
            }
            ret.append(tmp);
        }
        return ret.toString().toUpperCase();
    }
}

Related

  1. byteToHex(byte data)
  2. byteToHex(byte[] array)
  3. byteToHex(byte[] array, String separator)
  4. byteToHex(byte[] b, int size)
  5. byteToHex(byte[] base)
  6. byteToHex(byte[] buffer)
  7. ByteToHex(byte[] bytes)
  8. byteToHex(byte[] content, int nLength)
  9. byteToHex(byte[] raw)