欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java 基礎(chǔ) byte[]與各種數(shù)據(jù)類型互相轉(zhuǎn)換的簡單示例

 更新時(shí)間:2017年01月04日 15:07:32   投稿:lqh  
這篇文章主要介紹了Java 基礎(chǔ) byte[]與各種數(shù)據(jù)類型互相轉(zhuǎn)換的簡單示例的相關(guān)資料,這里對(duì)byte[]類型對(duì)long,int,double,float,short,cahr,object,string類型相互轉(zhuǎn)換的實(shí)例,需要的朋友可以參考下

Java 基礎(chǔ) byte[]與各種數(shù)據(jù)類型互相轉(zhuǎn)換的簡單示例

這里對(duì)byte[]類型對(duì)long,int,double,float,short,cahr,object,string類型相互轉(zhuǎn)換的實(shí)例,

在socket開發(fā)過程中,通常需要將一些具體的值(這些值可能是各種Java類型)轉(zhuǎn)化為byte[]類型,為此我總結(jié)了如下這個(gè)示例,貼出來,以便經(jīng)常翻看:

public class TestCase { 
   
  /** 
   * short到字節(jié)數(shù)組的轉(zhuǎn)換. 
   */ 
  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; 
  } 
 
  /** 
   * 字節(jié)數(shù)組到short的轉(zhuǎn)換. 
   */ 
  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到字節(jié)數(shù)組的轉(zhuǎn)換. 
   */ 
  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; 
  } 
 
  /** 
   * 字節(jié)數(shù)組到int的轉(zhuǎn)換. 
   */ 
  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類型轉(zhuǎn)成byte數(shù)組 
   */ 
  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; 
  } 
 
  /** 
   * 字節(jié)數(shù)組到long的轉(zhuǎn)換. 
   */ 
  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到字節(jié)數(shù)組的轉(zhuǎn)換. 
   */ 
  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; 
  } 
   
  /** 
   * 字節(jié)數(shù)組到double的轉(zhuǎn)換. 
   */ 
  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到字節(jié)數(shù)組的轉(zhuǎn)換. 
   */ 
  public static void floatToByte(float x) { 
    //先用 Float.floatToIntBits(f)轉(zhuǎn)換成int 
  } 
   
  /** 
   * 字節(jié)數(shù)組到float的轉(zhuǎn)換. 
   */ 
  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到字節(jié)數(shù)組的轉(zhuǎn)換. 
   */ 
   public static byte[] charToByte(char c){ 
    byte[] b = new byte[2]; 
    b[0] = (byte) ((c & 0xFF00) >> 8); 
    b[1] = (byte) (c & 0xFF); 
    return b; 
   } 
    
   /** 
   * 字節(jié)數(shù)組到char的轉(zhuǎn)換. 
   */ 
   public static char byteToChar(byte[] b){ 
    char c = (char) (((b[0] & 0xFF) << 8) | (b[1] & 0xFF)); 
    return c; 
   } 
   
  /** 
   * string到字節(jié)數(shù)組的轉(zhuǎn)換. 
   */ 
  public static byte[] stringToByte(String str) throws UnsupportedEncodingException{ 
    return str.getBytes("GBK"); 
  } 
   
  /** 
   * 字節(jié)數(shù)組到String的轉(zhuǎn)換. 
   */ 
  public static String bytesToString(byte[] str) { 
    String keyword = null; 
    try { 
      keyword = new String(str,"GBK"); 
    } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
    } 
    return keyword; 
  } 
   
   
  /** 
   * object到字節(jié)數(shù)組的轉(zhuǎn)換 
   */ 
  @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); 
 
  } 
 
  /** 
   * 字節(jié)數(shù)組到object的轉(zhuǎn)換. 
   */ 
  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; 
  } 
 
} 

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

  • 如何處理@PathVariable中的特殊字符問題

    如何處理@PathVariable中的特殊字符問題

    這篇文章主要介紹了如何處理@PathVariable中的特殊字符問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • Mybatis中的@Param及動(dòng)態(tài)SQL詳解

    Mybatis中的@Param及動(dòng)態(tài)SQL詳解

    這篇文章主要介紹了Mybatis中的@Param及動(dòng)態(tài)SQL詳解,@Param是MyBatis所提供的作為Dao層的注解,作用是用于傳遞參數(shù),從而可以與SQL中的的字段名相對(duì)應(yīng),需要的朋友可以參考下
    2023-10-10
  • 解決調(diào)試JDK源碼時(shí),不能查看變量的值問題

    解決調(diào)試JDK源碼時(shí),不能查看變量的值問題

    下面小編就為大家?guī)硪黄鉀Q調(diào)試JDK源碼時(shí),不能查看變量的值問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06
  • 分析java 中AspectJ切面執(zhí)行兩次的原因

    分析java 中AspectJ切面執(zhí)行兩次的原因

    這篇文章主要介紹了分析java 中AspectJ切面執(zhí)行兩次的原因的相關(guān)資料,希望通過本能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • Java并發(fā)之原子性 有序性 可見性及Happen Before原則

    Java并發(fā)之原子性 有序性 可見性及Happen Before原則

    一提到happens-before原則,就讓人有點(diǎn)“丈二和尚摸不著頭腦”。這個(gè)涵蓋了整個(gè)JMM中可見性原則的規(guī)則,究竟如何理解,把我個(gè)人一些理解記錄下來。下面可以和小編一起學(xué)習(xí)Java 并發(fā)四個(gè)原則
    2021-09-09
  • Java線程關(guān)閉的3種方法

    Java線程關(guān)閉的3種方法

    這篇文章介紹了Java線程關(guān)閉的3種方法,有需要的朋友可以參考一下
    2013-10-10
  • spring security中的csrf防御原理(跨域請(qǐng)求偽造)

    spring security中的csrf防御原理(跨域請(qǐng)求偽造)

    這篇文章主要介紹了spring security中的csrf防御機(jī)制原理解析(跨域請(qǐng)求偽造),本文通過實(shí)例代碼詳解的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-12-12
  • java 使用策略模式操作JDBC數(shù)據(jù)庫

    java 使用策略模式操作JDBC數(shù)據(jù)庫

    這篇文章主要介紹了java 使用策略模式操作JDBC數(shù)據(jù)庫的相關(guān)資料,這里提供實(shí)例實(shí)現(xiàn)對(duì)JDBC數(shù)據(jù)庫的操作增刪改查的功能,需要的朋友可以參考下
    2017-08-08
  • Java 反射機(jī)制

    Java 反射機(jī)制

    這篇文章簡要的說明了Java的反射機(jī)制,Java的反射是框架設(shè)計(jì)的靈魂,本文通過例子能看的更加清晰的理解
    2021-06-06
  • Java函數(shù)式編程之通過行為參數(shù)化傳遞代碼

    Java函數(shù)式編程之通過行為參數(shù)化傳遞代碼

    行為參數(shù)化就是可以幫助你處理頻繁變更的需求的一種軟件開發(fā)模式,這篇文章將給大家詳細(xì)的介紹一下Java函數(shù)式編程之行為參數(shù)化傳遞代碼,感興趣的同學(xué)可以參考閱讀下
    2023-08-08

最新評(píng)論