Example usage for java.lang Long toBinaryString

List of usage examples for java.lang Long toBinaryString

Introduction

In this page you can find the example usage for java.lang Long toBinaryString.

Prototype

public static String toBinaryString(long i) 

Source Link

Document

Returns a string representation of the long argument as an unsigned integer in base 2.

Usage

From source file:com.gigabytedevelopersinc.app.calculator.Calculator.java

private String newBase(String originalNumber, int originalBase, int base) throws SyntaxException {
    String[] split = originalNumber.split("\\.");
    if (split.length == 0) {
        split = new String[1];
        split[0] = "0";
    }//from w  w  w  .java 2  s  .c om
    if (split[0].isEmpty()) {
        split[0] = "0";
    }
    if (originalBase != 10) {
        split[0] = Long.toString(Long.parseLong(split[0], originalBase));
    }

    String wholeNumber = "";
    switch (base) {
    case 2:
        wholeNumber = Long.toBinaryString(Long.parseLong(split[0]));
        break;
    case 8:
        wholeNumber = Long.toOctalString(Long.parseLong(split[0]));
        break;
    case 10:
        wholeNumber = split[0];
        break;
    case 16:
        wholeNumber = Long.toHexString(Long.parseLong(split[0]));
        break;
    }
    if (split.length == 1)
        return wholeNumber.toUpperCase(Locale.US);

    // Catch overflow (it's a decimal, it can be (slightly) rounded
    if (split[1].length() > 13) {
        split[1] = split[1].substring(0, 13);
    }

    Solver solver = new Solver();
    double decimal = 0;
    if (originalBase != 10) {
        String decimalFraction = Long.toString(Long.parseLong(split[1], originalBase)) + "/" + originalBase
                + "^" + split[1].length();
        decimal = solver.eval(decimalFraction);
    } else {
        decimal = Double.parseDouble("0." + split[1]);
    }
    if (decimal == 0)
        return wholeNumber.toUpperCase(Locale.US);

    String decimalNumber = "";
    for (int i = 0; decimal != 0 && i <= 8; i++) {
        decimal *= base;
        int id = (int) Math.floor(decimal);
        decimal -= id;
        decimalNumber += Integer.toHexString(id);
    }
    return (wholeNumber + "." + decimalNumber).toUpperCase(Locale.US);
}

From source file:com.ah.ui.actions.config.ImportCsvFileAction.java

/**
 * Check the netmask format//from ww w.  ja  v  a  2 s .  c  o m
 *
 * @param arg_Netmask -
 * @return boolean - true:the netmask is wrong;false:the netmask is right.
 */
public static boolean getNetmaskWrongFlag(String arg_Netmask) {
    if (!getIpAddressWrongFlag(arg_Netmask)) {
        long long_Net = AhEncoder.ip2Long(arg_Netmask);
        String binaryString = Long.toBinaryString(long_Net);

        return binaryString.length() != 32
                || (binaryString.contains("0") && binaryString.indexOf("0") < binaryString.lastIndexOf("1"));
    } else {
        return true;
    }
}