Java BigInteger to bigIntegerToAddress(BigInteger ethereumAddress)

Here you can find the source of bigIntegerToAddress(BigInteger ethereumAddress)

Description

Get the address as short string.

License

Apache License

Parameter

Parameter Description
ethereumAddress Address as big integer.

Return

short string represent 1f21c...

Declaration

public static String bigIntegerToAddress(BigInteger ethereumAddress) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.math.BigInteger;

public class Main {
    /**/* www  .j  a  v  a2 s  .c om*/
     * Get the address as short string.
     * @param ethereumAddress Address as big integer.
     * @return short string represent 1f21c...
     */
    public static String bigIntegerToAddress(BigInteger ethereumAddress) {
        String address = ethereumAddress.toString(16);

        while (address.length() < 40) {
            address = "0" + address;
        }

        if (!isValidAddress(address)) {
            throw new Error("not an address");
        }

        return "0x" + address;
    }

    /**
     * Returns true if address is a valid ethereum address.
     * @param address to check
     * @return true if valid
     */
    public static boolean isValidAddress(byte[] address) {
        return address != null && address.length == 20;
    }

    /**
     * Returns true if address is a valid ethereum address.
     * @param address to check
     * @return true if valid
     */
    public static boolean isValidAddress(String address) {
        return address != null && address.length() == 40;
    }
}

Related

  1. bigIntegerToBase64(BigInteger value)
  2. bigIntegerToBitArray(BigInteger x, int length)
  3. BigIntegerToByteArrayWithoutSign(BigInteger value)
  4. bigIntegerToBytes(BigInteger b, int numBytes)