Java Byte Array to String byteToMacString(byte[] data)

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

Description

byte To Mac String

License

Open Source License

Declaration

public static String byteToMacString(byte[] data) 

Method Source Code

//package com.java2s;
/*/*from   ww  w.ja  v  a  2 s.  co  m*/
 * Copyright (c) 2015 Yoyodyne, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

public class Main {
    public static String byteToMacString(byte[] data) {
        return hexStringToColonSeparatedString(bytesToHexString(data));
    }

    public static String hexStringToColonSeparatedString(String input) {
        if (input == null) {
            return null;
        }
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < input.length(); i += 2) {
            buf.append(input.charAt(i));
            buf.append(input.charAt(i + 1));
            if (i + 2 < input.length())
                buf.append(":");
        }
        return buf.toString();
    }

    public static String bytesToHexString(byte[] bytes) {

        if (bytes == null) {
            return "null";
        }

        String ret = "";
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            if (i > 0) {
                ret += ":";
            }
            short u8byte = (short) (bytes[i] & 0xff);
            String tmp = Integer.toHexString(u8byte);
            if (tmp.length() == 1) {
                buf.append("0");
            }
            buf.append(tmp);
        }
        ret = buf.toString();
        return ret;
    }
}

Related

  1. bytesToString(long b)
  2. bytesToString(long bytes)
  3. bytesToString(long bytes)
  4. bytesToString(long size)
  5. byteTo16String(byte[] bt)
  6. byteToStr(byte[] byteArray)
  7. byteToStr(byte[] byteArray)
  8. ByteToString(byte[] a, int nLen)
  9. byteToString(byte[] array, int byteLength)