Byte Converter : Byte « Date Type « Android

Home
Android
1.2D Graphics
2.Animation
3.Core Class
4.Database
5.Date Type
6.Development
7.File
8.Game
9.Hardware
10.Media
11.Network
12.Security
13.UI
14.User Event
Android » Date Type » Byte 
Byte Converter
   
//package net.capstone.cra.smartcontroller.util;

/**
 * The Class ByteConverter.
 */
class ByteConverter {

  /**
   * Int2byte.
   * integer type convert to byte type
   
   @param i the i
   @return the byte[]
   */
  public static byte[] int2byte(int i) {
    byte[] array = new byte[4];
    array[3(byte) (i & 0xff);
    array[2(byte) ((i >> 80xff);
    array[1(byte) ((i >> 160xff);
    array[0(byte) ((i >> 240xff);

    return array;
  }

  /**
   * Byte2int.
   * byte type convert to integer type.
   *
   @param bytes the bytes
   @param start the start
   @return the int
   */
  public static int byte2int(byte[] bytes, int start) {
    int newValue = 0;
  
    newValue |= (((intbytes[start + 0]) << 240xFF000000;
    newValue |= (((intbytes[start + 1]) << 160xFF0000;
    newValue |= (((intbytes[start + 2]) << 80xFF00;
    newValue |= (((intbytes[start + 3])) 0xFF;
  
    return newValue;
  }

  /**
   * Float2bytes.
   * float type convert to byte type. 
   *
   @param value the value
   @return the byte[]
   */
  public static byte[] float2bytes(float value) {
  
    byte[] array = new byte[4];
    int intBits = Float.floatToIntBits(value);
  
    array[0(byte) ((intBits & 0x000000ff>> 0);
    array[1(byte) ((intBits & 0x0000ff00>> 8);
    array[2(byte) ((intBits & 0x00ff0000>> 16);
    array[3(byte) ((intBits & 0xff000000>> 24);
  
    return array;
  }

  /**
   * Byte2float.
   * byte type convert to float type
   *
   @param arr the arr
   @param start the start
   @return the float
   */
  public static float byte2float(byte[] arr, int start) {
    int i = 0;
    int len = 4;
    int cnt = 0;
    byte[] tmp = new byte[len];
  
    for (i = start; i < (start + len); i++) {
      tmp[cnt= arr[i];
      cnt++;
    }
  
    int accum = 0;
    i = 0;
    for (int shiftBy = 0; shiftBy < 32; shiftBy += 8) {
      accum |= ((long) (tmp[i0xff)) << shiftBy;
      i++;
    }
    return Float.intBitsToFloat(accum);
  }
}

   
    
    
  
Related examples in the same category
1.Four Bytes To Long
2.Byte Convert
3.unsigned Byte To Int
4.Bytes to KB
5.byte To Unsigned Int
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.