List of usage examples for java.lang Integer toBinaryString
public static String toBinaryString(int i)
From source file:Main.java
public static String bytesToBits(byte[] b) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < b.length; i++) { String bits = Integer.toBinaryString(b[i] & 0xFF); while (bits.length() < 8) { bits = "0" + bits; }/* w w w. j av a 2 s .c om*/ if (i > 0) { sb.append(" "); } sb.append(bits); } return sb.toString(); }
From source file:Main.java
/** * Convert decimalism to other.//from w ww .j av a2s. c om * @param decimal Value in decimalism. * @param radix Which system to convert. (Only 2, 8, 16) */ public static String toBits(int decimal, int radix) { String num; switch (radix) { case 2: num = "00000000000000000000000000000000" + Integer.toBinaryString(decimal); return "0b" + num.substring(num.length() - 32); case 8: num = "00000000000" + Integer.toOctalString(decimal); return "0" + num.substring(num.length() - 11); case 16: num = "00000000" + Integer.toHexString(decimal); return "0x" + num.substring(num.length() - 8); default: return UNKNOWN; } }
From source file:Main.java
/** * 7 => 00000111//w ww . j a v a2 s . c o m * * @param src * @return */ public static String getBinaryString(byte src) { String binaryString = Integer.toBinaryString(src); if (binaryString.length() > Byte.SIZE) { binaryString = binaryString.substring(binaryString.length() - Byte.SIZE); } else { String temp = ""; for (int i = 0; i < Byte.SIZE - binaryString.length(); i++) { temp += "0"; } binaryString = temp + binaryString; } return binaryString; }
From source file:Main.java
/** * Return a BCD representation of an integer as a 4-byte array. * @param i int//w ww . j av a 2 s .c o m * @return byte[4] */ public static byte[] intToBcdArray(int i) { if (i < 0) throw new IllegalArgumentException("Argument cannot be a negative integer."); StringBuilder binaryString = new StringBuilder(); while (true) { int quotient = i / 10; int remainder = i % 10; String nibble = String.format("%4s", Integer.toBinaryString(remainder)).replace(' ', '0'); binaryString.insert(0, nibble); if (quotient == 0) { break; } else { i = quotient; } } return ByteBuffer.allocate(4).putInt(Integer.parseInt(binaryString.toString(), 2)).array(); }
From source file:Main.java
public static String byteToBit(byte... bytes) { StringBuffer sb = new StringBuffer(); int z, len;//w w w .ja v a2s. c om String str; for (int w = 0; w < bytes.length; w++) { z = bytes[w]; z |= 256; str = Integer.toBinaryString(z); len = str.length(); sb.append(str.substring(len - 8, len)); } return sb.toString(); }
From source file:edu.rit.flick.genetics.util.HexPrinter.java
public static String intToBinaryString(final int i) { final StringBuilder str = new StringBuilder( org.apache.commons.lang.StringUtils.leftPad(Integer.toBinaryString(i), 16, '0')); for (int idx = str.length() - 4; idx > 0; idx -= 4) str.insert(idx, "_"); return str.toString(); }
From source file:org.openhab.binding.phc.internal.PHCHelper.java
/** * Get the ThingUID by the given parameters. * * @param thingTypeUID//from w ww. jav a 2s . c o m * @param moduleAddr reverse (to the reverse address - DIP switches) * @return */ public static ThingUID getThingUIDreverse(ThingTypeUID thingTypeUID, byte moduleAddr) { // convert to 5-bit binary string and reverse in second step String thingID = StringUtils.leftPad(StringUtils.trim(Integer.toBinaryString(moduleAddr & 0xFF)), 5, '0'); thingID = new StringBuilder(thingID).reverse().toString(); ThingUID thingUID = new ThingUID(thingTypeUID, thingID); return thingUID; }
From source file:Main.java
/** * 7 => 00000111/*w w w .j a v a 2s. com*/ * * @param src * @return */ public static String getBinaryString(byte src) { String binaryString = Integer.toBinaryString(src); if (binaryString.length() > Byte.SIZE) { binaryString = binaryString.substring(binaryString.length() - Byte.SIZE); } else { StringBuffer sb = new StringBuffer(); for (int i = 0; i < Byte.SIZE - binaryString.length(); i++) { sb.append("0"); } binaryString = sb.toString() + binaryString; } return binaryString; }
From source file:org.openhab.binding.phc.internal.PHCHelper.java
/** * Convert the byte b into an binary String * * @param b//from w ww. j a v a 2s .co m * @return */ public static Object byteToBinaryString(byte b) { return StringUtils.leftPad(StringUtils.trim(Integer.toBinaryString(b & 0xFF)), 8, '0') + " "; }
From source file:org.mabb.fontverter.woff.WoffOutputStream.java
public void writeUIntBase128(int num) throws IOException { List<Byte> bytes = new ArrayList<Byte>(); String binary = Integer.toBinaryString(num); while (!binary.isEmpty()) { if (binary.length() < 7) binary = StringUtils.repeat("0", 7 - (binary.length() % 7)) + binary; String byteBinary = binary.substring(binary.length() - 7, binary.length()); // last (or only) byte signficant bit must be 0 all others sig bit must be 1 int sigbit = bytes.size() == 0 ? 0 : 128; byte byteOn = (byte) (sigbit + Integer.parseInt(byteBinary, 2)); bytes.add(0, byteOn);// w ww . ja v a 2 s.c o m binary = binary.substring(0, binary.length() - 7); } for (byte byteOn : bytes) write(byteOn); }