欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java 基础 byte[]与各种数据类型互相转换的简单示例

程序员文章站 2024-03-09 13:36:23
java 基础 byte[]与各种数据类型互相转换的简单示例 这里对byte[]类型对long,int,double,float,short,cahr,object,st...

java 基础 byte[]与各种数据类型互相转换的简单示例

这里对byte[]类型对long,int,double,float,short,cahr,object,string类型相互转换的实例,

在socket开发过程中,通常需要将一些具体的值(这些值可能是各种java类型)转化为byte[]类型,为此我总结了如下这个示例,贴出来,以便经常翻看:

public class testcase { 
   
  /** 
   * short到字节数组的转换. 
   */ 
  public static byte[] shorttobyte(short number) { 
    int temp = number; 
    byte[] b = new byte[2]; 
    for (int i = 0; i < b.length; i++) { 
      b[i] = new integer(temp & 0xff).bytevalue();// 将最低位保存在最低位 
      temp = temp >> 8;// 向右移8位 
    } 
    return b; 
  } 
 
  /** 
   * 字节数组到short的转换. 
   */ 
  public static short bytetoshort(byte[] b) { 
    short s = 0; 
    short s0 = (short) (b[0] & 0xff);// 最低位 
    short s1 = (short) (b[1] & 0xff); 
    s1 <<= 8; 
    s = (short) (s0 | s1); 
    return s; 
  } 
   
   
  /** 
   * int到字节数组的转换. 
   */ 
  public static byte[] inttobyte(int number) { 
    int temp = number; 
    byte[] b = new byte[4]; 
    for (int i = 0; i < b.length; i++) { 
      b[i] = new integer(temp & 0xff).bytevalue();// 将最低位保存在最低位 
      temp = temp >> 8;// 向右移8位 
    } 
    return b; 
  } 
 
  /** 
   * 字节数组到int的转换. 
   */ 
  public static int bytetoint(byte[] b) { 
    int s = 0; 
    int s0 = b[0] & 0xff;// 最低位 
    int s1 = b[1] & 0xff; 
    int s2 = b[2] & 0xff; 
    int s3 = b[3] & 0xff; 
    s3 <<= 24; 
    s2 <<= 16; 
    s1 <<= 8; 
    s = s0 | s1 | s2 | s3; 
    return s; 
  } 
   
   
  /** 
   * long类型转成byte数组 
   */ 
  public static byte[] longtobyte(long number) { 
    long temp = number; 
    byte[] b = new byte[8]; 
    for (int i = 0; i < b.length; i++) { 
      b[i] = new long(temp & 0xff).bytevalue();// 将最低位保存在最低位 temp = temp 
                            // >> 8;// 向右移8位 
    } 
    return b; 
  } 
 
  /** 
   * 字节数组到long的转换. 
   */ 
  public static long bytetolong(byte[] b) { 
    long s = 0; 
    long s0 = b[0] & 0xff;// 最低位 
    long s1 = b[1] & 0xff; 
    long s2 = b[2] & 0xff; 
    long s3 = b[3] & 0xff; 
    long s4 = b[4] & 0xff;// 最低位 
    long s5 = b[5] & 0xff; 
    long s6 = b[6] & 0xff; 
    long s7 = b[7] & 0xff; 
 
    // s0不变 
    s1 <<= 8; 
    s2 <<= 16; 
    s3 <<= 24; 
    s4 <<= 8 * 4; 
    s5 <<= 8 * 5; 
    s6 <<= 8 * 6; 
    s7 <<= 8 * 7; 
    s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7; 
    return s; 
  } 
   
  /** 
   * double到字节数组的转换. 
   */ 
  public static byte[] doubletobyte(double num) {  
    byte[] b = new byte[8];  
    long l = double.doubletolongbits(num);  
    for (int i = 0; i < 8; i++) {  
      b[i] = new long(l).bytevalue();  
      l = l >> 8;  
    } 
    return b; 
  } 
   
  /** 
   * 字节数组到double的转换. 
   */ 
  public static double getdouble(byte[] b) {  
    long m;  
    m = b[0];  
    m &= 0xff;  
    m |= ((long) b[1] << 8);  
    m &= 0xffff;  
    m |= ((long) b[2] << 16);  
    m &= 0xffffff;  
    m |= ((long) b[3] << 24);  
    m &= 0xffffffffl;  
    m |= ((long) b[4] << 32);  
    m &= 0xffffffffffl;  
    m |= ((long) b[5] << 40);  
    m &= 0xffffffffffffl;  
    m |= ((long) b[6] << 48);  
    m &= 0xffffffffffffffl;  
    m |= ((long) b[7] << 56);  
    return double.longbitstodouble(m);  
  } 
   
   
  /** 
   * float到字节数组的转换. 
   */ 
  public static void floattobyte(float x) { 
    //先用 float.floattointbits(f)转换成int 
  } 
   
  /** 
   * 字节数组到float的转换. 
   */ 
  public static float getfloat(byte[] b) {  
    // 4 bytes  
    int accum = 0;  
    for ( int shiftby = 0; shiftby < 4; shiftby++ ) {  
        accum |= (b[shiftby] & 0xff) << shiftby * 8;  
    }  
    return float.intbitstofloat(accum);  
  }  
 
   /** 
   * char到字节数组的转换. 
   */ 
   public static byte[] chartobyte(char c){ 
    byte[] b = new byte[2]; 
    b[0] = (byte) ((c & 0xff00) >> 8); 
    b[1] = (byte) (c & 0xff); 
    return b; 
   } 
    
   /** 
   * 字节数组到char的转换. 
   */ 
   public static char bytetochar(byte[] b){ 
    char c = (char) (((b[0] & 0xff) << 8) | (b[1] & 0xff)); 
    return c; 
   } 
   
  /** 
   * string到字节数组的转换. 
   */ 
  public static byte[] stringtobyte(string str) throws unsupportedencodingexception{ 
    return str.getbytes("gbk"); 
  } 
   
  /** 
   * 字节数组到string的转换. 
   */ 
  public static string bytestostring(byte[] str) { 
    string keyword = null; 
    try { 
      keyword = new string(str,"gbk"); 
    } catch (unsupportedencodingexception e) { 
      e.printstacktrace(); 
    } 
    return keyword; 
  } 
   
   
  /** 
   * object到字节数组的转换 
   */ 
  @test 
  public void testobject2bytearray() throws ioexception, 
      classnotfoundexception { 
    // object obj = ""; 
    integer[] obj = { 1, 3, 4 }; 
 
    // // object to bytearray 
    bytearrayoutputstream bo = new bytearrayoutputstream(); 
    objectoutputstream oo = new objectoutputstream(bo); 
    oo.writeobject(obj); 
    byte[] bytes = bo.tobytearray(); 
    bo.close(); 
    oo.close(); 
    system.out.println(arrays.tostring(bytes)); 
 
    integer[] intarr = (integer[]) testbytearray2object(bytes); 
    system.out.println(arrays.aslist(intarr)); 
 
 
    byte[] b2 = inttobyte(123); 
    system.out.println(arrays.tostring(b2)); 
 
    int a = bytetoint(b2); 
    system.out.println(a); 
 
  } 
 
  /** 
   * 字节数组到object的转换. 
   */ 
  private object testbytearray2object(byte[] bytes) throws ioexception, 
      classnotfoundexception { 
    // byte[] bytes = null; 
    object obj; 
    // bytearray to object 
    bytearrayinputstream bi = new bytearrayinputstream(bytes); 
    objectinputstream oi = new objectinputstream(bi); 
    obj = oi.readobject(); 
    bi.close(); 
    oi.close(); 
    system.out.println(obj); 
    return obj; 
  } 
 
} 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!