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

 更新时间:2017年01月04日 15:07:32   投稿:lqh  
这篇文章主要介绍了Java 基础 byte[]与各种数据类型互相转换的简单示例的相关资料,这里对byte[]类型对long,int,double,float,short,cahr,object,string类型相互转换的实例,需要的朋友可以参考下

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; 
  } 
 
} 

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

相关文章

  • 详解SpringBoot整合MyBatis详细教程

    详解SpringBoot整合MyBatis详细教程

    这篇文章主要介绍了详解SpringBoot整合MyBatis详细教程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • Java swing实现应用程序对数据库的访问问题

    Java swing实现应用程序对数据库的访问问题

    这篇文章主要介绍了Java swing实现应用程序对数据库的访问,本次实验需要做一个GUI界面和一个连接查询功能,在论坛上借鉴了其他大佬获取网站内容的部分代码,然后自己做了一个及其简陋的swing界面,算是把这个实验完成了,需要的朋友可以参考下
    2022-09-09
  • SpringBoot 整合jdbc和mybatis的方法

    SpringBoot 整合jdbc和mybatis的方法

    该文章主要为记录如何在SpringBoot项目中整合JDBC和MyBatis,在整合中我会使用简单的用法和测试用例,感兴趣的朋友跟随小编一起看看吧
    2019-11-11
  • java使用内存数据库ssdb的步骤

    java使用内存数据库ssdb的步骤

    这篇文章主要介绍了java使用内存数据库ssdb的步骤,帮助大家更好的理解和使用Java,感兴趣的朋友可以了解下
    2020-12-12
  • SpringBoot整合RabbitMQ实现交换机与队列的绑定

    SpringBoot整合RabbitMQ实现交换机与队列的绑定

    这篇文章将通过几个实例为大家介绍一些SpringBoot中RabbitMQ如何绑定交换机(交换器)与队列,文中的示例代码讲解详细,感兴趣的可以了解一下
    2022-05-05
  • 在Java的Struts框架下进行web编程的入门教程

    在Java的Struts框架下进行web编程的入门教程

    这篇文章主要介绍了在Java的Struts框架下进行web编程的入门教程,需要的朋友可以参考下
    2015-11-11
  • 解析idea内嵌浏览器翻译

    解析idea内嵌浏览器翻译

    这篇文章主要介绍了解析idea内嵌浏览器翻译的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-10-10
  • 剑指Offer之Java算法习题精讲链表专题篇

    剑指Offer之Java算法习题精讲链表专题篇

    跟着思路走,之后从简单题入手,反复去看,做过之后可能会忘记,之后再做一次,记不住就反复做,反复寻求思路和规律,慢慢积累就会发现质的变化
    2022-03-03
  • SpringBoot集成Sharding Jdbc使用复合分片的实践

    SpringBoot集成Sharding Jdbc使用复合分片的实践

    数据库分库分表中间件是采用的 apache sharding。本文主要介绍了SpringBoot集成Sharding Jdbc使用复合分片的实践,具有一定的参考价值,感兴趣的可以了解一下
    2021-09-09
  • Spring注解驱动之BeanDefinitionRegistryPostProcessor原理解析

    Spring注解驱动之BeanDefinitionRegistryPostProcessor原理解析

    这篇文章主要介绍了Spring注解驱动之BeanDefinitionRegistryPostProcessor原理,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-09-09

最新评论