Java Hex Calculate toHex(byte[] val, int start, int len)

Here you can find the source of toHex(byte[] val, int start, int len)

Description

Can be used to encode values that contain invalid XML characters.

License

Sun Public License Notice

Parameter

Parameter Description
val data to be converted
start offset
len count

Declaration

public static String toHex(byte[] val, int start, int len) 

Method Source Code

//package com.java2s;
/*/*from w  w w . j av  a 2s.c o m*/
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2005 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

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

    /**
     * Can be used to encode values that contain invalid XML characters.
     * At SAX parser end must be used pair method to get original value.
     *
     * @param val data to be converted
     * @param start offset
     * @param len count
     *
     * @since 1.29
     */
    public static String toHex(byte[] val, int start, int len) {
        StringBuffer buf = new StringBuffer();

        for (int i = 0; i < len; i++) {
            byte b = val[start + i];
            buf.append(DEC2HEX[(b & 0xf0) >> 4]);
            buf.append(DEC2HEX[b & 0x0f]);
        }

        return buf.toString();
    }
}

Related

  1. ToHex(byte[] src)
  2. toHex(byte[] src)
  3. toHex(byte[] text)
  4. toHex(byte[] v)
  5. toHex(byte[] val)
  6. toHex(byte[] value)
  7. toHex(byte[] value)
  8. toHex(char c)
  9. toHex(final byte b)