Example usage for java.lang Integer toBinaryString

List of usage examples for java.lang Integer toBinaryString

Introduction

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

Prototype

public static String toBinaryString(int i) 

Source Link

Document

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

Usage

From source file:Main.java

public static String decimalIntToBinaryString(int i) {
    String str = "0";
    try {//from  w w  w .j a v  a  2s.c  o  m
        str = Integer.toBinaryString(i);
    } catch (NumberFormatException e) {
        e.printStackTrace();
    }
    return str;
}

From source file:Main.java

public static String byteToBits(byte b) {
    String bits = Integer.toBinaryString(b & 0xFF);
    while (bits.length() < 8) {
        bits = "0" + bits;
    }/*  w  ww  .j a v  a2  s.  co m*/
    return bits;
}

From source file:Main.java

public static List<Integer> getBitsFromIntagerNumber(Integer num) {
    String binary = Integer.toBinaryString(num);
    List<Integer> list = new ArrayList<Integer>(0);
    if (num == 0)
        list.add(num);//from   w  w w  . j a  v a 2s.  c  o m
    for (int i = 0; i < binary.length(); i++) {
        if ((int) binary.charAt(i) == 49)
            list.add((int) Math.pow(2, binary.length() - 1 - i));
    }
    return list;
}

From source file:Main.java

public static String getBinaryString(byte[] bytes) {
    String result = "";
    for (byte b : bytes) {
        result = result + String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0');
    }//from   w w w .  j  av  a2s.co m
    return result;
}

From source file:Main.java

private static String byte2bitsReverse(byte ch) {
    //Log.e(TAG, "byte2bitsReverse() ch = " + ch);
    int z = ch;// w  ww. jav  a 2s  . c o  m
    z |= 256;
    String str = Integer.toBinaryString(z);
    int len = str.length();
    String result = str.substring(len - 8, len);
    StringBuffer sb = new StringBuffer(result);
    //Log.e(TAG, "byte2bitsReverse() return = " + sb.reverse().toString());
    return sb.reverse().toString();
}

From source file:Main.java

public static String getWriteType(int writeType) {
    StringBuilder permissionStr = new StringBuilder();
    String binaryString = Integer.toBinaryString(writeType);
    binaryString = String.format("%16s", binaryString).replace(' ', '0');
    int len = binaryString.length();
    //WRITE_TYPE_NO_RESPONSE (0x00000001)
    if (binaryString.charAt(len - 1) == '1') {
        permissionStr.append("NO_RESPONSE,");
    }//from  w w  w  .  j  a  v  a2  s.  c om
    //WRITE_TYPE_DEFAULT (0x00000002)
    if (binaryString.charAt(len - 2) == '1') {
        permissionStr.append("WRITE,");
    }
    //WRITE_TYPE_SIGNED (0x00000004)
    if (binaryString.charAt(len - 3) == '1') {
        permissionStr.append("SIGNED,");
    }

    String perStr = permissionStr.toString();
    if (perStr.endsWith(",")) {
        perStr = perStr.substring(0, perStr.length() - 1);
    }

    return perStr;
}

From source file:Main.java

public static String getProperties(int properties) {
    StringBuilder permissionStr = new StringBuilder();
    String binaryString = Integer.toBinaryString(properties);
    binaryString = String.format("%16s", binaryString).replace(' ', '0');
    int len = binaryString.length();
    //PROPERTY_BROADCAST (0x00000001)
    if (binaryString.charAt(len - 1) == '1') {
        permissionStr.append("BROADCAST,");
    }//from  www  .  j  a  v  a 2  s. c o  m
    //PROPERTY_READ (0x00000002)
    if (binaryString.charAt(len - 2) == '1') {
        permissionStr.append("READ,");
    }
    //PROPERTY_WRITE_NO_RESPONSE (0x00000004)
    if (binaryString.charAt(len - 3) == '1') {
        permissionStr.append("WRITE_NO_RESPONSE,");
    }
    //PROPERTY_WRITE (0x00000008)
    if (binaryString.charAt(len - 4) == '1') {
        permissionStr.append("READ_ENCRYPTED_MITM,");
    }

    //PROPERTY_NOTIFY (0x00000010)
    if (binaryString.charAt(len - 5) == '1') {
        permissionStr.append("NOTIFY,");
    }
    //PROPERTY_INDICATE (0x00000020)
    if (binaryString.charAt(len - 6) == '1') {
        permissionStr.append("INDICATE,");
    }
    //PROPERTY_SIGNED_WRITE (0x00000040)
    if (binaryString.charAt(len - 7) == '1') {
        permissionStr.append("SIGNED_WRITE,");
    }

    //PROPERTY_EXTENDED_PROPS (0x00000080)
    if (binaryString.charAt(len - 8) == '1') {
        permissionStr.append("EXTENDED_PROPS,");
    }

    String perStr = permissionStr.toString();
    if (perStr.endsWith(",")) {
        perStr = perStr.substring(0, perStr.length() - 1);
    }

    return perStr;
}

From source file:Main.java

public static String getPermission(int permission) {
    StringBuilder permissionStr = new StringBuilder();
    String binaryString = Integer.toBinaryString(permission);
    binaryString = String.format("%16s", binaryString).replace(' ', '0');
    int len = binaryString.length();
    //PERMISSION_READ (0x00000001)
    if (binaryString.charAt(len - 1) == '1') {
        permissionStr.append("READ,");
    }//from  w w w  .j av a 2  s . c  om
    //PERMISSION_READ_ENCRYPTED (0x00000002)
    if (binaryString.charAt(len - 2) == '1') {
        permissionStr.append("READ_ENCRYPTED,");
    }
    //PERMISSION_READ_ENCRYPTED_MITM (0x00000004)
    if (binaryString.charAt(len - 3) == '1') {
        permissionStr.append("READ_ENCRYPTED_MITM,");
    }

    //PERMISSION_WRITE (0x00000010)
    if (binaryString.charAt(len - 5) == '1') {
        permissionStr.append("WRITE,");
    }
    //PERMISSION_WRITE_ENCRYPTED (0x00000020)
    if (binaryString.charAt(len - 6) == '1') {
        permissionStr.append("WRITE_ENCRYPTED,");
    }
    //PERMISSION_WRITE_ENCRYPTED_MITM (0x00000040)
    if (binaryString.charAt(len - 7) == '1') {
        permissionStr.append("WRITE_ENCRYPTED_MITM,");
    }

    //PERMISSION_WRITE_SIGNED (0x00000080)
    if (binaryString.charAt(len - 8) == '1') {
        permissionStr.append("WRITE_ENCRYPTED_MITM,");
    }
    //PERMISSION_WRITE_SIGNED_MITM (0x00000100)
    if (binaryString.charAt(len - 9) == '1') {
        permissionStr.append("WRITE_SIGNED_MITM,");
    }

    String perStr = permissionStr.toString();
    if (perStr.endsWith(",")) {
        perStr = perStr.substring(0, perStr.length() - 1);
    }

    return perStr;
}

From source file:Main.java

public static String hexString2binaryString(String hexString) {
    if (hexString == null || hexString.length() % 2 != 0)
        return null;
    String bString = "", tmp;
    for (int i = 0; i < hexString.length(); i++) {
        tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16));
        bString += tmp.substring(tmp.length() - 4);
    }//from   ww w.  java  2s  .  c om
    return bString;
}

From source file:Main.java

public static String hexString2BinaryString(String hexString) {
    if (hexString == null || hexString.length() % 2 != 0)
        return null;
    String bString = "", tmp;
    for (int i = 0; i < hexString.length(); i++) {
        tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16));
        bString += tmp.substring(tmp.length() - 4);
    }/*  w  w w .ja va 2s  .  co  m*/
    return bString;
}