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:com.sangupta.passcode.HashStream.java

private static String asBinary(byte[] hash) {
    String binary = "";
    for (byte b : hash) {
        int unsigned = b & 0xFF;
        String bin = Integer.toBinaryString(unsigned);
        bin = StringUtils.leftPad(bin, 8, '0');
        binary = binary + bin;/*  w w w.  j a v  a 2s .c om*/
    }

    return binary;
}

From source file:org.squidy.nodes.laserpointer.proxy.ProxyTest.java

public void run() {
    while (running) {
        if (!connected) {
            try {
                sock = new Socket("localhost", 6666);
                //sock.setTcpNoDelay(true);
                input = sock.getInputStream();
                output = sock.getOutputStream();
                connected = true;//from w  w w .jav  a  2 s . c  o  m
            } catch (UnknownHostException e) {
                logger.error("Couldn't initiate connection.");
            } catch (IOException e) {
            }
        }
        if (running) {
            try {
                while (connected) {
                    int inByte;
                    while ((inByte = input.read()) == 0xFF) {
                        logger.debug("Received: " + Integer.toBinaryString(inByte) + "[" + inByte + "]");
                        // send bytes
                        byte[] bytes = new byte[640 * 512 * 3];
                        r.nextBytes(bytes);
                        for (int i = 0; i < bytes.length; i++) {
                            if (i % 3 == 0) {
                                bytes[i + 1] = 0; // G
                                bytes[i + 2] = 0; // R
                            }
                        }
                        output.write(bytes);
                    }
                }
            } catch (IOException e) {
                logger.error("Connection error occured.");
                connected = false;
            }
        }
    }
}

From source file:org.openhab.binding.intertechno.internal.parser.V3Parser.java

@Override
public void parseConfig(List<String> configParts) throws BindingConfigParseException {
    String id = "";
    Boolean group = false;//  ww  w .j a va  2 s .c  o m
    Integer channelID = 0;

    // Extract parameter values from config parts
    for (int i = 0; i < configParts.size(); i++) {
        String paramName = configParts.get(i).split("=")[0].toLowerCase();
        String paramValue = configParts.get(i).split("=")[1];

        switch (paramName) {
        case "id":
            id = paramValue;
            break;
        case "group":
            if (paramValue.equals("1") || paramValue.toLowerCase().equals("true")) {
                group = true;
            }
            break;
        case "channel":
            try {
                channelID = Integer.parseInt(paramValue);
            } catch (NumberFormatException e) {
                throw new BindingConfigParseException("Channel ID (" + paramValue + ") is not a number.");
            }
        }
    }

    // Check parameter values
    if (id.length() != 26) {
        throw new BindingConfigParseException("The ID must contain exactly 26 digits!");
    }

    for (int i = 0; i < id.length(); i++) {
        if (id.charAt(i) != '0' && id.charAt(i) != '1') {
            throw new BindingConfigParseException("The ID must contains only the digits 1 and 0!");
        }
    }

    if (channelID < 0 || channelID > 15) {
        throw new BindingConfigParseException("The channel ID must be in a range from 0 to 15!");
    }

    // Build command strings
    String channelIDCode = StringUtils.leftPad(Integer.toBinaryString(channelID), 4, "0");

    commandON = id + getGroupCode(group) + "1" + channelIDCode;
    commandOFF = id + getGroupCode(group) + "0" + channelIDCode;

    logger.trace("commandON = {}", commandON);
    logger.trace("commandOFF = {}", commandOFF);
}

From source file:com.shadows.hkprogrammer.core.MessageHandler.java

private void WritePayloadOfParameterMessage(ParameterMessage message, ByteArray messageBytes) {
    ByteArray payload = new ByteArray(65);
    String Model = Integer.toBinaryString(message.getTXModelType().getValue()),
            Craft = Integer.toBinaryString(message.getCraftTypeNum().getValue());
    payload.Write(Integer.parseInt(StringUtils.leftPad(Model, 4, "0") + StringUtils.leftPad(Craft, 4, "0"), 2));
    String Bitmask = "";
    for (Boolean reverse : message.getReverseBitmask())
        Bitmask = (reverse ? 1 : 0) + Bitmask;
    Bitmask = "00" + Bitmask;
    payload.Write((byte) Integer.parseInt(Bitmask, 2));
    for (ParameterDRValue DRValue : message.getDRValues()) {
        payload.Write((byte) DRValue.getOnValue());
        payload.Write((byte) DRValue.getOffValue());
    }//from w  w w .  ja  v  a2s  .  co m
    for (int Swash : message.getSwash())
        payload.Write((byte) Swash);
    for (PotmeterEndPoint Endpoint : message.getEndPoints()) {
        payload.Write((byte) Endpoint.getLeft());
        payload.Write((byte) Endpoint.getRigth());
    }
    for (ThrottleCurve Curve : message.getThrottleCurves()) {
        payload.Write((byte) Curve.getNormal());
        payload.Write((byte) Curve.getID());
    }
    for (PitchCurve Curve : message.getPitchCurves()) {
        payload.Write((byte) Curve.getNormal());
        payload.Write((byte) Curve.getID());
    }
    for (int Subtrim : message.getSubtrim())
        payload.Write((byte) Subtrim);
    for (MixSetting Mix : message.getMixes()) {
        String Source = Integer.toBinaryString(Mix.getSource().getValue()),
                Destination = Integer.toBinaryString(Mix.getDestination().getValue());
        payload.Write((byte) Integer
                .parseInt(StringUtils.leftPad(Source, 4, "0") + StringUtils.leftPad(Destination, 4, "0"), 2));
        payload.Write((byte) Mix.getUprate());
        payload.Write((byte) Mix.getDownrate());
        payload.Write((byte) Mix.getSwitch().getValue());
    }
    for (SwitchFunction Switch : message.getSwitchFunction())
        payload.Write((byte) Switch.getValue());
    for (VRFunction VR : message.getVRModes())
        payload.Write((byte) VR.getValue());
    messageBytes.Write(payload);
    messageBytes.Write(payload.ChecksumBySB());
}

From source file:org.skife.muckery.mappy.ByteUtils.java

/**
 * Translate the given byte array into a string of 1s and 0s
 *
 * @param bytes The bytes to translate/*from w  w w .j  a  v  a2 s . c o m*/
 * @return The string
 */
public static String toBinaryString(byte[] bytes) {
    StringBuilder buffer = new StringBuilder();
    for (byte b : bytes) {
        String bin = Integer.toBinaryString(0xFF & b);
        bin = bin.substring(0, Math.min(bin.length(), 8));

        for (int j = 0; j < 8 - bin.length(); j++) {
            buffer.append('0');
        }

        buffer.append(bin);
    }
    return buffer.toString();
}

From source file:org.pentaho.chart.plugin.jfreechart.utils.JFreeChartUtils.java

/**
 * @param data//from   ww w.java2s . c o m
 * @param column
 * @return
 */
public static String getColumnName(final ChartTableModel data, final int column) {
    return data.getColumnName(column) == null ? Integer.toBinaryString(column) : data.getColumnName(column);
}

From source file:org.efaps.admin.datamodel.attributetype.BitEnumType.java

/**
 * @param _int integer the BitSet is wanted for
 * @return BitSet representing the given integer
 *///from  w w w.j  a  va  2s  . co  m
public static BitSet getBitSet(final int _int) {
    final char[] bits = Integer.toBinaryString(_int).toCharArray();
    ArrayUtils.reverse(bits);
    final BitSet bitSet = new BitSet(bits.length);
    for (int i = 0; i < bits.length; i++) {
        if (bits[i] == '1') {
            bitSet.set(i, true);
        } else {
            bitSet.set(i, false);
        }
    }
    return bitSet;
}

From source file:sec_algo.aud_sec.java

public BufferedWriter getAudioStream() {
    FileInputStream fin = null;/*w  w w  .j a va  2s  . c  om*/
    BufferedWriter audstream = null;

    try {
        fin = new FileInputStream(this.file);
        //           audstream = new BufferedWriter(new FileWriter(returnFileName()+"_ex."+returnFileExt()));
        //           byte contents[] = new byte[100];
        //           while(fin.read(contents)!= -1){
        //               System.out.println("reading & writing from file");
        //               for(byte b : contents)
        //                   for(int x = 0; x < 8; x++)
        //                       audstream.write(b>>x & 1);
        //           }
        //           System.out.println("Finished!");
        //           System.out.println("audstream contents: " + audstream.toString());
        byte[] header = new byte[8];
        fin.read(header);
        fin.close();
        //           System.out.println("header bytes: " + Arrays.toString(header));
        ArrayList<String> bitstring = new ArrayList<String>();
        for (int i = 0; i < header.length; i++)
            bitstring.add(String.format("%8s", Integer.toBinaryString(header[i] & 0xFF)).replace(' ', '0'));
        System.out.print("bit input: [/");
        for (int i = 0; i < bitstring.size(); i++) {
            System.out.print(bitstring.get(i) + " ");
        }
        System.out.println("]/");

        System.out.println(bitstring.get(0) + " " + bitstring.get(1) + " " + bitstring.get(2));
        System.out.println("Bitrate index: " + bitstring.get(2).substring(0, 4));

        AudioInputStream in = AudioSystem.getAudioInputStream(this.file);
        AudioInputStream din = null;
        AudioFormat baseFormat = in.getFormat();
        AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(),
                getBitrate(bitstring.get(2).substring(0, 4)), baseFormat.getChannels(),
                baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
        din = AudioSystem.getAudioInputStream(decodedFormat, in);
        int size = din.available();
        byte[] bytaud = new byte[size];
        din.read(bytaud);
        bitstring = new ArrayList<String>();
        for (int i = 0; i < header.length; i++)
            bitstring.add(String.format("%8s", Integer.toBinaryString(header[i] & 0xFF)).replace(' ', '0'));
        System.out.print("bit input: [/");
        for (int i = 0; i < bitstring.size(); i++) {
            System.out.print(bitstring.get(i) + " ");
        }
        System.out.println("]/");
        in.close();
        din.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return audstream;
}

From source file:io.bitsquare.gui.util.validation.altcoins.ByteballAddressValidator.java

private static String buffer2bin(byte[] buf) {
    StringBuffer bytes = new StringBuffer();
    for (int i = 0; i < buf.length; i++) {
        String bin = String.format("%8s", Integer.toBinaryString(buf[i] & 0xFF)).replace(' ', '0');
        bytes.append(bin);/* w w  w  .  ja  va2s .  c  o m*/
    }
    return bytes.toString();
}

From source file:com.github.utils.mycollect.util.EmojiUtil.java

/**
 * Unicode??//w  w  w  .  jav  a2s .  c  o m
 *
 * @param one
 * @param two
 * @return ??codePoint
 */
private static Integer codePointToExtUnicode(int one, int two) {

    if (one > 0 && two > 0) {
        //U+D80055296,U+DC0056320
        String binStrHigh = Integer.toBinaryString(one - 55296);
        String binStrLow = Integer.toBinaryString(two - 56320);
        if (StringUtils.isNotBlank(binStrHigh) && StringUtils.isNotBlank(binStrLow)) {
            StringBuilder binStrRes = new StringBuilder();
            binStrRes.append(StringUtils.right(binStrHigh, 10));
            if (StringUtils.length(binStrLow) < 10) {
                binStrRes.append(StringUtils.leftPad(binStrLow, 10, "0"));
            } else {
                binStrRes.append(StringUtils.right(binStrLow, 10));
            }
            Integer resInt = Integer.parseInt(binStrRes.toString(), 2);
            //U+10000
            return resInt + 65536;
        }
    }

    return 0;
}