Java Integer to Hex convertIntArrayToHexString(int[] arr)

Here you can find the source of convertIntArrayToHexString(int[] arr)

Description

Formats a byte array in hex, without the "0x" notation.

License

Open Source License

Parameter

Parameter Description
arr a parameter

Declaration

public static String convertIntArrayToHexString(int[] arr) 

Method Source Code

//package com.java2s;
/**//from w  w w.  j  a v  a2  s. c  o m
 * Copyright (c) 2009 Andrew Rapp. All rights reserved.
 *  
 * This file is part of XBee-XMPP
 *  
 * XBee-XMPP 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.
 *  
 * XBee-XMPP 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.
 *  
 * You should have received a copy of the GNU General Public License
 * along with XBee-XMPP.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Formats a byte array in hex, without the "0x" notation.
     * 
     * @param arr
     * @return
     */
    public static String convertIntArrayToHexString(int[] arr) {

        //       log.debug("packet is " + ByteUtils.toBase16(arr));

        StringBuffer strbuf = new StringBuffer();

        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > 255) {
                throw new IllegalArgumentException(
                        "value in array exceeds one byte: " + arr[i]);
            }

            strbuf.append(padHex(arr[i]));
        }

        return strbuf.toString();
    }

    /**
     * The hex string must 2 chars per byte for it to be parsed correctly, 
     * so 0x1 is represented as 01
     * 
     * @param b
     * @return
     */
    public static String padHex(int b) {
        if (b < 0x10) {
            return "0" + Integer.toHexString(b);
        } else {
            return Integer.toHexString(b);
        }
    }
}

Related

  1. asHex(final int value)
  2. asHex(int i)
  3. asHex(int i, int width)
  4. asHex(int val)
  5. convertIntToHex(int i)
  6. convertInttoHexa(int n)
  7. int2CharHex(int integer)
  8. int2hex(final int a, final StringBuffer str)