Java Hex Calculate toHex4(int value)

Here you can find the source of toHex4(int value)

Description

to Hex

License

Open Source License

Declaration

public static String toHex4(int value) 

Method Source Code

//package com.java2s;
/*/* w  w  w  .j a  v a2  s .c o m*/
  HexUtils.java
    
  (c) 2010-2013 Edward Swartz
    
  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
 */

public class Main {
    public static String toHex4(int value) {
        if (value < 0)
            value &= 0xffff;
        return padAddress(Integer.toHexString(value & 0x7fffffff).toUpperCase());
    }

    /**
     * @param string
     * @return
     */
    public static String padAddress(String string) {
        final byte[] zeroes = { '0', '0', '0', '0' };
        int len = 4 - string.length();
        if (len > 0) {
            return new String(zeroes, 0, len) + string;
        } else {
            return string;
        }
    }
}

Related

  1. toHex(StringBuilder s, byte b)
  2. toHex00String(int c)
  3. toHEX1(byte b)
  4. toHex16(int w)
  5. toHex16(long l)
  6. toHex8(long x)
  7. toHexa(byte[] bb)
  8. toHexAddress(long address)
  9. toHexaDecimal(byte[] bytesToConvert)