Java Hex Calculate toHexStringLE(byte n)

Here you can find the source of toHexStringLE(byte n)

Description

to Hex String LE

License

Open Source License

Declaration

public static String toHexStringLE(byte n) 

Method Source Code

//package com.java2s;
/*-//from www.  j  a  v  a2s. c o m
 * Copyright (C) 2006-2007 Erik Larsson
 * 
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static String toHexStringLE(byte n) {
        return byteArrayToHexString(toByteArrayLE(n));
    }

    public static String toHexStringLE(short n) {
        return byteArrayToHexString(toByteArrayLE(n));
    }

    public static String toHexStringLE(char n) {
        return byteArrayToHexString(toByteArrayLE(n));
    }

    public static String toHexStringLE(int n) {
        return byteArrayToHexString(toByteArrayLE(n));
    }

    public static String toHexStringLE(long n) {
        return byteArrayToHexString(toByteArrayLE(n));
    }

    public static String byteArrayToHexString(byte[] array) {
        return byteArrayToHexString(array, 0, array.length);
    }

    public static String byteArrayToHexString(byte[] array, int offset, int length) {
        String result = "";
        for (int i = offset; i < (offset + length); ++i) {
            byte b = array[i];
            String currentHexString = Integer.toHexString(b & 0xFF);
            if (currentHexString.length() == 1)
                currentHexString = "0" + currentHexString;
            result += currentHexString;
        }
        return result;
    }

    public static byte[] toByteArrayLE(byte b) {
        byte[] result = new byte[1];
        result[0] = b;
        return result;
    }

    public static byte[] toByteArrayLE(short s) {
        byte[] result = new byte[2];
        result[0] = (byte) ((s >> 0) & 0xFF);
        result[1] = (byte) ((s >> 8) & 0xFF);
        return result;
    }

    public static byte[] toByteArrayLE(char c) {
        byte[] result = new byte[2];
        result[0] = (byte) ((c >> 0) & 0xFF);
        result[1] = (byte) ((c >> 8) & 0xFF);
        return result;
    }

    public static byte[] toByteArrayLE(int i) {
        byte[] result = new byte[4];
        result[0] = (byte) ((i >> 0) & 0xFF);
        result[1] = (byte) ((i >> 8) & 0xFF);
        result[2] = (byte) ((i >> 16) & 0xFF);
        result[3] = (byte) ((i >> 24) & 0xFF);
        return result;
    }

    public static byte[] toByteArrayLE(long l) {
        byte[] result = new byte[8];
        result[0] = (byte) ((l >> 0) & 0xFF);
        result[1] = (byte) ((l >> 8) & 0xFF);
        result[2] = (byte) ((l >> 16) & 0xFF);
        result[3] = (byte) ((l >> 24) & 0xFF);
        result[4] = (byte) ((l >> 32) & 0xFF);
        result[5] = (byte) ((l >> 40) & 0xFF);
        result[6] = (byte) ((l >> 48) & 0xFF);
        result[7] = (byte) ((l >> 56) & 0xFF);
        return result;
    }
}

Related

  1. toHexString(StringBuffer buffer, byte[] bytes, int numBytes)
  2. toHexString0x(byte[] byteArray)
  3. toHexString2(int i)
  4. toHexString3(byte aByte)
  5. toHexStringBE(char[] array)
  6. toHexStringUpperCase(byte[] b)
  7. toHexTable(byte[] byteSrc, int lengthOfLine)
  8. toHexText(byte[] bytes, int length)
  9. toHexUtil(int n)