Java Hex Calculate toHex(int n, Boolean bigEndian)

Here you can find the source of toHex(int n, Boolean bigEndian)

Description

Outputs the hex value of a int, allowing the developer to specify the endinaness in the process.

License

Open Source License

Parameter

Parameter Description
n The int value to output as hex
bigEndian Flag to output the int as big or little endian

Return

A string of length 8 corresponding to the hex representation of n ( minus the leading "0x" )

Declaration


public static String toHex(int n, Boolean bigEndian) 

Method Source Code

//package com.java2s;

public class Main {
    /** String for quick lookup of a hex character based on index */
    private static String hexChars = "0123456789abcdef";

    /**/* ww  w. ja v  a  2  s.c  om*/
     * Outputs the hex value of a int, allowing the developer to specify
     * the endinaness in the process.  Hex output is lowercase.
     *
     * @param n The int value to output as hex
     * @param bigEndian Flag to output the int as big or little endian
     * @return A string of length 8 corresponding to the 
     *      hex representation of n ( minus the leading "0x" )
     * @langversion ActionScript 3.0
     * @playerversion Flash 9.0
     * @tiptext
     */

    public static String toHex(int n, Boolean bigEndian) {
        String s = "";

        if (bigEndian) {
            for (int i = 0; i < 4; i++) {
                s += hexChars.charAt((n >> ((3 - i) * 8 + 4)) & 0xF) + hexChars.charAt((n >> ((3 - i) * 8)) & 0xF);
            }
        } else {
            for (int x = 0; x < 4; x++) {
                s += hexChars.charAt((n >> (x * 8 + 4)) & 0xF) + hexChars.charAt((n >> (x * 8)) & 0xF);
            }
        }

        return s;
    }
}

Related

  1. toHex(int i)
  2. toHex(int i, String $default)
  3. toHex(int myByte)
  4. toHex(int n)
  5. toHex(int n)
  6. toHex(int n, int bytes)
  7. toHex(int n, int s)
  8. toHex(int nibble)
  9. toHex(int num)