Java/Data Type/byte

Материал из Java эксперт
Перейти к: навигация, поиск

Содержание

byte Array To Hex String

   <source lang="java">
  

public class Main {

 public static void main(String arg[]) {
   byteArrayToHexString(("abc").getBytes());
 }
 public static String byteArrayToHexString(byte[] b) {
   StringBuffer sb = new StringBuffer(b.length * 2);
   for (int i = 0; i < b.length; i++) {
     int v = b[i] & 0xff;
     if (v < 16) {
       sb.append("0");
     }
     sb.append(Integer.toHexString(v));
   }
   return sb.toString().toUpperCase();
 }

}


 </source>
   
  
 
  



Byte class creates primitives that wrap themselves around data items of the byte data type

   <source lang="java">
   
     

public class MainClass {

 public static void main(String[] args) {
   byte by = (byte) "A";
   Byte by2 = new Byte(by);
   System.out.println(by2.byteValue());
 }

}


 </source>
   
  
 
  



Compare Two Java byte Arrays Example

   <source lang="java">
  

import java.util.Arrays; public class Main {

 public static void main(String[] args) {
   byte[] a1 = new byte[] { 7, 25, 12 };
   byte[] a2 = new byte[] { 7, 25, 12 };
   System.out.println(Arrays.equals(a1, a2));
 }

}


 </source>
   
  
 
  



Convert an UNSIGNED byte to a JAVA type

   <source lang="java">
  

public class Main {

 public static void main(String args[]) {
   byte b1 = 127;
   System.out.println(b1);
   System.out.println(unsignedByteToInt(b1));
 }
 public static int unsignedByteToInt(byte b) {
   return (int) b & 0xFF;
 }

}


 </source>
   
  
 
  



Convert byte[ ] array to String

   <source lang="java">
  

public class Main {

   public static void main(String[] args) {
       byte[] byteArray = new byte[] {87, 79, 87, 46, 46, 46};
       String value = new String(byteArray);
       System.out.println(value);
   }

} //WOW...


 </source>
   
  
 
  



Convert Byte to numeric primitive data types example

   <source lang="java">
  

public class Main {

 public static void main(String[] args) {
   Byte bObj = new Byte("10");
   byte b = bObj.byteValue();
   System.out.println(b);
   short s = bObj.shortValue();
   System.out.println(s);
   int i = bObj.intValue();
   System.out.println(i);
   float f = bObj.floatValue();
   System.out.println(f);
   double d = bObj.doubleValue();
   System.out.println(d);
   long l = bObj.longValue();
   System.out.println(l);
 }

}


 </source>
   
  
 
  



Convert byte to String: Creating a byte array and passing it to the String constructor

   <source lang="java">
  

public class Main {

   public static void main(String[] args) {
       byte b = 65;
       
       System.out.println(new String(new byte[] {b}));
   }

} //A


 </source>
   
  
 
  



Convert byte to String: Using simple concatenation with an empty String

   <source lang="java">
  

public class Main {

   public static void main(String[] args) {
       byte b = 65;
       
       System.out.println(b + "");
   }

} //65


 </source>
   
  
 
  



Convert byte to String: Using the static toString method of the Byte class

   <source lang="java">
  

public class Main {

   public static void main(String[] args) {
       byte b = 65;
       
       System.out.println(Byte.toString(b));
   }

}


 </source>
   
  
 
  



Convert Java String to Byte example

   <source lang="java">
  

public class Main {

 public static void main(String[] args) {
   Byte bObj1 = new Byte("100");
   System.out.println(bObj1);
   Byte bObj2 = Byte.valueOf("100");
   System.out.println(bObj2);
 }

}


 </source>
   
  
 
  



Convert String to byte

   <source lang="java">
  

public class Main {

 public static void main(String[] args) {
   String s = "65";
   byte b = Byte.valueOf(s);
   System.out.println(b);
   // Causes a NumberFormatException since the value is out of range
   System.out.println(Byte.valueOf("129"));
 }

} /* 65 Exception in thread "main" java.lang.NumberFormatException: Value out of range. Value:"129" Radix:10

 at java.lang.Byte.parseByte(Byte.java:153)
 at java.lang.Byte.valueOf(Byte.java:184)
 at java.lang.Byte.valueOf(Byte.java:208)
 at Main.main(Main.java:11)
  • /


 </source>
   
  
 
  



Convert String to byte array

   <source lang="java">
  

public class Main {

   public static void main(String[] args) {
       String stringToConvert = "this is a test";
       
       byte[] theByteArray = stringToConvert.getBytes();
       
       System.out.println(theByteArray.length);
   }

} //14


 </source>
   
  
 
  



Convert String to primitive byte Example

   <source lang="java">
  

public class Main {

 public static void main(String[] args) {
   String str = new String("10");
   byte b = Byte.parseByte(str);
   System.out.println(b);
 }

}


 </source>
   
  
 
  



Gets the maximum of three byte values.

   <source lang="java">
  

/*

* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
* 
*      http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**

*

Provides extra functionality for Java Number classes.

*
* @author 
* @since 2.0
* @version $Id: NumberUtils.java 609475 2008-01-06 23:58:59Z bayard $
*/

public class Main {

 /**
*

Gets the maximum of three byte values.

  * 
  * @param a  value 1
  * @param b  value 2
  * @param c  value 3
  * @return  the largest of the values
  */
 public static byte max(byte a, byte b, byte c) {
     if (b > a) {
         a = b;
     }
     if (c > a) {
         a = c;
     }
     return a;
 }

}


 </source>
   
  
 
  



Gets the minimum of three byte values.

   <source lang="java">
  

/*

* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
* 
*      http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**

*

Provides extra functionality for Java Number classes.

*
* @author 
* @since 2.0
* @version $Id: NumberUtils.java 609475 2008-01-06 23:58:59Z bayard $
*/

public class Main {

 /**
*

Gets the minimum of three byte values.

  * 
  * @param a  value 1
  * @param b  value 2
  * @param c  value 3
  * @return  the smallest of the values
  */
 public static byte min(byte a, byte b, byte c) {
     if (b < a) {
         a = b;
     }
     if (c < a) {
         a = c;
     }
     return a;
 }

}


 </source>
   
  
 
  



hex String To Byte Array

   <source lang="java">
  

public class Main {

 public static byte[] hexStringToByteArray(String s) {
   byte[] b = new byte[s.length() / 2];
   for (int i = 0; i < b.length; i++) {
     int index = i * 2;
     int v = Integer.parseInt(s.substring(index, index + 2), 16);
     b[i] = (byte) v;
   }
   return b;
 }

}


 </source>
   
  
 
  



Immutable byte list

   <source lang="java">
  

/*

* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
* 
*      http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**

*
* @xerces.internal 
* 
* @author Ankit Pasricha, IBM
* 
* @version $Id: ByteListImpl.java 446747 2006-09-15 21:46:20Z mrglavas $
*/

public class ByteListImpl {

   // actually data stored in a byte array
   protected final byte[] data;
   
   // canonical representation of the data
   protected String canonical;
   
   public ByteListImpl(byte[] data) {
       this.data = data;
   }
   
   /**
    * The number of bytes in the list. The range of 
    * valid child object indices is 0 to length-1 inclusive. 
    */
   public int getLength() {
       return data.length;
   }
   /**
    * Checks if the byte item is a 
    * member of this list. 
    * @param item  byte whose presence in this list 
    *   is to be tested. 
    * @return  True if this list contains the byte 
    *   item. 
    */
   public boolean contains(byte item) {
       for (int i = 0; i < data.length; ++i) {
           if (data[i] == item) {
               return true;
           }
       }
       return false;
   }
   /**
    * Returns the indexth item in the collection. The index 
    * starts at 0. 
    * @param index  index into the collection. 
    * @return  The byte at the indexth 
    *   position in the ByteList. 
    * @exception XSException
    *   INDEX_SIZE_ERR: if index is greater than or equal to the 
    *   number of objects in the list.
    */
   public byte item(int index) 
       throws Exception {
       
       if(index < 0 || index > data.length - 1) {
           throw new RuntimeException("INDEX SIZE ERR");
       }
       return data[index];
   }
   

}


 </source>
   
  
 
  



Java byte: byte is smallest Java integer type.byte is 8 bit signed type ranges from �128 to 127.

   <source lang="java">
  

public class Main {

 public static void main(String[] args) {
   byte b1 = 100;
   byte b2 = 20;
   System.out.println("Value of byte variable b1 is :" + b1);
   System.out.println("Value of byte variable b1 is :" + b2);
 }

} /* Value of byte variable b1 is :100 Value of byte variable b1 is :20

  • /


 </source>
   
  
 
  



Java Byte Example

   <source lang="java">
  

public class Main {

 public static void main(String[] args) {
   byte b = 10;
   Byte bObj1 = new Byte(b);
   Byte bObj2 = new Byte("4");
   System.out.println(bObj1);
   System.out.println(bObj2);
 }

}


 </source>
   
  
 
  



Max and Min values of datatype char

   <source lang="java">
  

public class Main {

 public static void main(String[] args) {
   System.out.println((int) Character.MIN_VALUE);
   System.out.println((int) Character.MAX_VALUE);
 }

} //0 //65535


 </source>
   
  
 
  



Min and Max values of datatype byte

   <source lang="java">
  

public class Main {

   public static void main(String[] args) {
       System.out.println(Byte.MIN_VALUE);
       System.out.println(Byte.MAX_VALUE);
   }

} /* -128 127

  • /


 </source>
   
  
 
  



Min and Max values of datatype int

   <source lang="java">
  

public class Main {

 public static void main(String[] args) {
   System.out.println(Integer.MIN_VALUE);
   System.out.println(Integer.MAX_VALUE);
 }

} /* -2147483648 2147483647

  • /


 </source>
   
  
 
  



Min and Max values of datatype short

   <source lang="java">
  

public class Main {

   public static void main(String[] args) {
       System.out.println(Short.MIN_VALUE);
       System.out.println(Short.MAX_VALUE);
   }

} /* -32768 32767

  • /


 </source>
   
  
 
  



Minimum and maximum values of a byte

   <source lang="java">
  

public class Main {

   public static void main(String[] args) {
       System.out.println(Byte.MIN_VALUE);
       System.out.println(Byte.MAX_VALUE);
   }

} /* -128 127

  • /


 </source>
   
  
 
  



Print the limits of primitive types (e.g. byte, short, int ...) in Java

   <source lang="java">
  

public class Main {

 public static void main(String args[]) {
   System.out.println("Min byte value   = " + Byte.MIN_VALUE);
   System.out.println("Max byte value   = " + Byte.MAX_VALUE);
   System.out.println("Min short value  = " + Short.MIN_VALUE);
   System.out.println("Max short value  = " + Short.MAX_VALUE);
   System.out.println("Min int value    = " + Integer.MIN_VALUE);
   System.out.println("Max int value    = " + Integer.MAX_VALUE);
   System.out.println("Min float value  = " + Float.MIN_VALUE);
   System.out.println("Max float value  = " + Float.MAX_VALUE);
   System.out.println("Min double value = " + Double.MIN_VALUE);
   System.out.println("Max double value = " + Double.MAX_VALUE);
 }

}


 </source>
   
  
 
  



This shows something interesting about addition of byte variables

   <source lang="java">
  

/*

* Copyright (c) Ian F. Darwin, http://www.darwinsys.ru/, 1996-2002.
* All rights reserved. Software written by Ian F. Darwin and others.
* $Id: LICENSE,v 1.8 2004/02/09 03:33:38 ian Exp $
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the
*    documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS""
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* 
* Java, the Duke mascot, and all variants of Sun"s Java "steaming coffee
* cup" logo are trademarks of Sun Microsystems. Sun"s, and James Gosling"s,
* pioneering role in inventing and promulgating (and standardizing) the Java 
* language and environment is gratefully acknowledged.
* 
* The pioneering role of Dennis Ritchie and Bjarne Stroustrup, of AT&T, for
* inventing predecessor languages C and C++ is also gratefully acknowledged.
*/

/**

* This shows something interesting about addition of "byte" variables
* (which also applies to all integer types).
*/

public class ByteAdd {

 public static void main(String[] argv) {
   System.out.println("ByteAdd Program");
   // bytes are signed integer, so can range from -127 to +127
   byte b1 = 10, b2 = 127;  
   System.out.println("b1 + b2 = " + (b1+b2));
   b1 = 127;
   System.out.println("b1 + b2 = " + (b1+b2));
 }

}



 </source>
   
  
 
  



To convert a byte to it"s hexadecimal equivalent

   <source lang="java">
  

public class Main {

 public static void main(String[] argv) {
   System.out.println(byteToHex((byte) 123));
 }
 public static String byteToHex(byte b) {
   int i = b & 0xFF;
   return Integer.toHexString(i);
 }

} //7b


 </source>
   
  
 
  



Translates between byte arrays and strings of "0"s and "1"s.

   <source lang="java">
  

/*

* Copyright 2001-2004 The Apache Software Foundation.
* 
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* 
*      http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**

* Translates between byte arrays and strings of "0"s and "1"s.
* 
* @todo may want to add more bit vector functions like and/or/xor/nand
* @todo also might be good to generate boolean[] from byte[] et. cetera.
* 
* @author Apache Software Foundation
* @since 1.3
* @version $Id $
*/

public class BinaryCodec {

 /*
  * tried to avoid using ArrayUtils to minimize dependencies while using these
  * empty arrays - dep is just not worth it.
  */
 /** Empty char array. */
 private static final char[] EMPTY_CHAR_ARRAY = new char[0];
 /** Empty byte array. */
 private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
 /** Mask for bit 0 of a byte. */
 private static final int BIT_0 = 1;
 /** Mask for bit 1 of a byte. */
 private static final int BIT_1 = 0x02;
 /** Mask for bit 2 of a byte. */
 private static final int BIT_2 = 0x04;
 /** Mask for bit 3 of a byte. */
 private static final int BIT_3 = 0x08;
 /** Mask for bit 4 of a byte. */
 private static final int BIT_4 = 0x10;
 /** Mask for bit 5 of a byte. */
 private static final int BIT_5 = 0x20;
 /** Mask for bit 6 of a byte. */
 private static final int BIT_6 = 0x40;
 /** Mask for bit 7 of a byte. */
 private static final int BIT_7 = 0x80;
 private static final int[] BITS = { BIT_0, BIT_1, BIT_2, BIT_3, BIT_4, BIT_5, BIT_6, BIT_7 };
 /**
  * Converts an array of raw binary data into an array of ascii 0 and 1
  * characters.
  * 
  * @param raw
  *          the raw binary data to convert
  * @return 0 and 1 ascii character bytes one for each bit of the argument
  * @see org.apache.rumons.codec.BinaryEncoder#encode(byte[])
  */
 public byte[] encode(byte[] raw) {
   return toAsciiBytes(raw);
 }
 /**
  * Converts an array of raw binary data into an array of ascii 0 and 1 chars.
  * 
  * @param raw
  *          the raw binary data to convert
  * @return 0 and 1 ascii character chars one for each bit of the argument
  * @throws EncoderException
  *           if the argument is not a byte[]
  * @see org.apache.rumons.codec.Encoder#encode(java.lang.Object)
  */
 public Object encode(Object raw) throws RuntimeException {
   if (!(raw instanceof byte[])) {
     throw new RuntimeException("argument not a byte array");
   }
   return toAsciiChars((byte[]) raw);
 }
 /**
  * Decodes a byte array where each byte represents an ascii "0" or "1".
  * 
  * @param ascii
  *          each byte represents an ascii "0" or "1"
  * @return the raw encoded binary where each bit corresponds to a byte in the
  *         byte array argument
  * @throws DecoderException
  *           if argument is not a byte[], char[] or String
  * @see org.apache.rumons.codec.Decoder#decode(java.lang.Object)
  */
 public Object decode(Object ascii) throws RuntimeException {
   if (ascii == null) {
     return EMPTY_BYTE_ARRAY;
   }
   if (ascii instanceof byte[]) {
     return fromAscii((byte[]) ascii);
   }
   if (ascii instanceof char[]) {
     return fromAscii((char[]) ascii);
   }
   if (ascii instanceof String) {
     return fromAscii(((String) ascii).toCharArray());
   }
   throw new RuntimeException("argument not a byte array");
 }
 /**
  * Decodes a byte array where each byte represents an ascii "0" or "1".
  * 
  * @param ascii
  *          each byte represents an ascii "0" or "1"
  * @return the raw encoded binary where each bit corresponds to a byte in the
  *         byte array argument
  * @see org.apache.rumons.codec.Decoder#decode(Object)
  */
 public byte[] decode(byte[] ascii) {
   return fromAscii(ascii);
 }
 /**
  * Decodes a String where each char of the String represents an ascii "0" or
  * "1".
  * 
  * @param ascii
  *          String of "0" and "1" characters
  * @return the raw encoded binary where each bit corresponds to a byte in the
  *         byte array argument
  * @see org.apache.rumons.codec.Decoder#decode(Object)
  */
 public byte[] toByteArray(String ascii) {
   if (ascii == null) {
     return EMPTY_BYTE_ARRAY;
   }
   return fromAscii(ascii.toCharArray());
 }
 // ------------------------------------------------------------------------
 //
 // static codec operations
 //
 // ------------------------------------------------------------------------
 /**
  * Decodes a byte array where each char represents an ascii "0" or "1".
  * 
  * @param ascii
  *          each char represents an ascii "0" or "1"
  * @return the raw encoded binary where each bit corresponds to a char in the
  *         char array argument
  */
 public static byte[] fromAscii(char[] ascii) {
   if (ascii == null || ascii.length == 0) {
     return EMPTY_BYTE_ARRAY;
   }
   // get length/8 times bytes with 3 bit shifts to the right of the length
   byte[] l_raw = new byte[ascii.length >> 3];
   /*
    * We decr index jj by 8 as we go along to not recompute indices using
    * multiplication every time inside the loop.
    */
   for (int ii = 0, jj = ascii.length - 1; ii < l_raw.length; ii++, jj -= 8) {
     for (int bits = 0; bits < BITS.length; ++bits) {
       if (ascii[jj - bits] == "1") {
         l_raw[ii] |= BITS[bits];
       }
     }
   }
   return l_raw;
 }
 /**
  * Decodes a byte array where each byte represents an ascii "0" or "1".
  * 
  * @param ascii
  *          each byte represents an ascii "0" or "1"
  * @return the raw encoded binary where each bit corresponds to a byte in the
  *         byte array argument
  */
 public static byte[] fromAscii(byte[] ascii) {
   if (ascii == null || ascii.length == 0) {
     return EMPTY_BYTE_ARRAY;
   }
   // get length/8 times bytes with 3 bit shifts to the right of the length
   byte[] l_raw = new byte[ascii.length >> 3];
   /*
    * We decr index jj by 8 as we go along to not recompute indices using
    * multiplication every time inside the loop.
    */
   for (int ii = 0, jj = ascii.length - 1; ii < l_raw.length; ii++, jj -= 8) {
     for (int bits = 0; bits < BITS.length; ++bits) {
       if (ascii[jj - bits] == "1") {
         l_raw[ii] |= BITS[bits];
       }
     }
   }
   return l_raw;
 }
 /**
  * Converts an array of raw binary data into an array of ascii 0 and 1
  * character bytes - each byte is a truncated char.
  * 
  * @param raw
  *          the raw binary data to convert
  * @return an array of 0 and 1 character bytes for each bit of the argument
  * @see org.apache.rumons.codec.BinaryEncoder#encode(byte[])
  */
 public static byte[] toAsciiBytes(byte[] raw) {
   if (raw == null || raw.length == 0) {
     return EMPTY_BYTE_ARRAY;
   }
   // get 8 times the bytes with 3 bit shifts to the left of the length
   byte[] l_ascii = new byte[raw.length << 3];
   /*
    * We decr index jj by 8 as we go along to not recompute indices using
    * multiplication every time inside the loop.
    */
   for (int ii = 0, jj = l_ascii.length - 1; ii < raw.length; ii++, jj -= 8) {
     for (int bits = 0; bits < BITS.length; ++bits) {
       if ((raw[ii] & BITS[bits]) == 0) {
         l_ascii[jj - bits] = "0";
       } else {
         l_ascii[jj - bits] = "1";
       }
     }
   }
   return l_ascii;
 }
 /**
  * Converts an array of raw binary data into an array of ascii 0 and 1
  * characters.
  * 
  * @param raw
  *          the raw binary data to convert
  * @return an array of 0 and 1 characters for each bit of the argument
  * @see org.apache.rumons.codec.BinaryEncoder#encode(byte[])
  */
 public static char[] toAsciiChars(byte[] raw) {
   if (raw == null || raw.length == 0) {
     return EMPTY_CHAR_ARRAY;
   }
   // get 8 times the bytes with 3 bit shifts to the left of the length
   char[] l_ascii = new char[raw.length << 3];
   /*
    * We decr index jj by 8 as we go along to not recompute indices using
    * multiplication every time inside the loop.
    */
   for (int ii = 0, jj = l_ascii.length - 1; ii < raw.length; ii++, jj -= 8) {
     for (int bits = 0; bits < BITS.length; ++bits) {
       if ((raw[ii] & BITS[bits]) == 0) {
         l_ascii[jj - bits] = "0";
       } else {
         l_ascii[jj - bits] = "1";
       }
     }
   }
   return l_ascii;
 }
 /**
  * Converts an array of raw binary data into a String of ascii 0 and 1
  * characters.
  * 
  * @param raw
  *          the raw binary data to convert
  * @return a String of 0 and 1 characters representing the binary data
  * @see org.apache.rumons.codec.BinaryEncoder#encode(byte[])
  */
 public static String toAsciiString(byte[] raw) {
   return new String(toAsciiChars(raw));
 }

}


 </source>
   
  
 
  



Use Byte constructor to convert byte primitive type to Byte object

   <source lang="java">
  

public class Main {

 public static void main(String[] args) {
   byte i = 10;
   
   Byte bObj = new Byte(i);
   System.out.println(bObj);
 }

}


 </source>
   
  
 
  



Use toString method of Byte class to convert Byte into String

   <source lang="java">
  

public class Main {

 public static void main(String[] args) {
   Byte bObj = new Byte("10");
   
   String str = bObj.toString();
   System.out.println(str);
 }

}


 </source>
   
  
 
  



Wrapping a Primitive Type in a Wrapper Object: boolean, byte, char, short, int, long, float, double

   <source lang="java">
  

public class Main {

 public static void main(String[] argv) throws Exception {
   Boolean refBoolean = new Boolean(true);
   boolean bool = refBoolean.booleanValue();
   
   
   Byte refByte = new Byte((byte) 123);
   byte b = refByte.byteValue();
   
   
   Character refChar = new Character("x");
   char c = refChar.charValue();
   
   
   Short refShort = new Short((short) 123);
   short s = refShort.shortValue();
   
   
   Integer refInt = new Integer(123);
   int i = refInt.intValue();
   
   
   Long refLong = new Long(123L);
   long l = refLong.longValue();
   
   
   Float refFloat = new Float(12.3F);
   float f = refFloat.floatValue();
   
   
   Double refDouble = new Double(12.3D);
   double d = refDouble.doubleValue();
 }

}


 </source>