Java Binary Encode toBinaryString(byte b)

Here you can find the source of toBinaryString(byte b)

Description

NOTE the SDK supplies a Integer.toBinaryString but it is not formatted to a standard number of chars so it was not a good option.

License

Open Source License

Declaration

public static String toBinaryString(byte b) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing./*from w w  w.  j a  v  a2  s. com*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/

public class Main {
    /**
     * NOTE the SDK supplies a Integer.toBinaryString but it is not formatted to a standard number of chars so it was not
     * a good option.
     */
    public static String toBinaryString(byte b) {
        StringBuffer sb = new StringBuffer();
        sb.append((b >> 7 & 0x01));
        sb.append((b >> 6 & 0x01));
        sb.append((b >> 5 & 0x01));
        sb.append((b >> 4 & 0x01));
        sb.append((b >> 3 & 0x01));
        sb.append((b >> 2 & 0x01));
        sb.append((b >> 1 & 0x01));
        sb.append((b & 0x01));
        return sb.toString();
    }
}

Related

  1. toBinaryID(final String id)
  2. toBinaryIeee754String(long decimal)
  3. toBinaryIntArray(byte[] bytes, int bitOffset, int bitCount)
  4. toBinaryName(String className)
  5. toBinaryString(boolean[] array)
  6. toBinaryString(byte b)
  7. toBinaryString(byte... bytes)
  8. toBinaryString(byte[] array)
  9. toBinaryString(byte[] bytes)