Java Hex Calculate toHexString(byte[] bytes)

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

Description

to Hex String

License

Open Source License

Parameter

Parameter Description
bytes The array of bytes to convert to ASCII hex form.

Return

An ASCII hexadecimal numeric string representing the specified array of bytes.

Declaration

public static final String toHexString(byte[] bytes) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004, 2005 Sybase, 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
 *
 * Contributors:// w  w w .j  a v a 2  s.c o m
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/

public class Main {
    private static final char[] HexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
            'e', 'f' };

    /**
     * @param bytes The array of bytes to convert to ASCII hex form.
     * @return       An ASCII hexadecimal numeric string representing the
     *           specified array of bytes.
     */
    public static final String toHexString(byte[] bytes) {
        StringBuffer sb = new StringBuffer();
        sb.append("0x");
        int i;
        for (i = 0; i < bytes.length; i++) {
            sb.append(HexChars[(bytes[i] >> 4) & 0xf]);
            sb.append(HexChars[bytes[i] & 0xf]);
        }
        return new String(sb);
    }
}

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)