Java Hex Calculate toHexString(byte[] bytes)

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

Description

to Hex String

License

Open Source License

Declaration

public static String toHexString(byte[] bytes) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004 Actuate Corporation.
 * 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
 *
 * Contributors://from  w  w w. java 2 s .  c  o  m
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

public class Main {
    public static String toHexString(byte[] bytes) {
        StringBuffer result = new StringBuffer();
        int length = 0;
        for (int i = 0; i < bytes.length; i++) {
            result.append(toHexString(bytes[i]));
            length += 2;
            if (length > 80) {
                result.append("\n");
                length = 0;
            }
        }
        return result.toString();
    }

    private static String toHexString(byte b) {
        String result;
        result = Integer.toHexString(toInt(b));
        if (result.length() == 1) {
            result = "0" + result;
        }
        return result;
    }

    public static String toHexString(int c) {
        final String[] padding = { "0", "00", "000" };
        String result = Integer.toHexString(c);
        if (result.length() < 4) {
            result = padding[3 - result.length()] + result;
        }
        return result;
    }

    private static int toInt(byte b) {
        return 0xff & b;
    }
}

Related

  1. toHexString(byte[] bytes)
  2. toHexString(byte[] bytes)
  3. toHexString(byte[] bytes)
  4. toHexString(byte[] bytes)
  5. toHexString(byte[] bytes)
  6. toHexString(byte[] bytes)
  7. toHexString(byte[] bytes)
  8. toHexString(byte[] bytes)
  9. toHexString(byte[] bytes)