Java基本类型与byte数组之间相互转换方法

 更新时间:2016年08月23日 10:16:17   投稿:jingxian  
下面小编就为大家带来一篇Java基本类型与byte数组之间相互转换方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

Java基本类型与byte数组之间相互转换,刚刚写的

package cn.teaey.utils;

import java.nio.charset.Charset;

public class ByteUtil
{
  public static byte[] getBytes(short data)
  {
    byte[] bytes = new byte[2];
    bytes[0] = (byte) (data & 0xff);
    bytes[1] = (byte) ((data & 0xff00) >> 8);
    return bytes;
  }


  public static byte[] getBytes(char data)
  {
    byte[] bytes = new byte[2];
    bytes[0] = (byte) (data);
    bytes[1] = (byte) (data >> 8);
    return bytes;
  }


  public static byte[] getBytes(int data)
  {
    byte[] bytes = new byte[4];
    bytes[0] = (byte) (data & 0xff);
    bytes[1] = (byte) ((data & 0xff00) >> 8);
    bytes[2] = (byte) ((data & 0xff0000) >> 16);
    bytes[3] = (byte) ((data & 0xff000000) >> 24);
    return bytes;
  }


  public static byte[] getBytes(long data)
  {
    byte[] bytes = new byte[8];
    bytes[0] = (byte) (data & 0xff);
    bytes[1] = (byte) ((data >> 8) & 0xff);
    bytes[2] = (byte) ((data >> 16) & 0xff);
    bytes[3] = (byte) ((data >> 24) & 0xff);
    bytes[4] = (byte) ((data >> 32) & 0xff);
    bytes[5] = (byte) ((data >> 40) & 0xff);
    bytes[6] = (byte) ((data >> 48) & 0xff);
    bytes[7] = (byte) ((data >> 56) & 0xff);
    return bytes;
  }


  public static byte[] getBytes(float data)
  {
    int intBits = Float.floatToIntBits(data);
    return getBytes(intBits);
  }


  public static byte[] getBytes(double data)
  {
    long intBits = Double.doubleToLongBits(data);
    return getBytes(intBits);
  }


  public static byte[] getBytes(String data, String charsetName)
  {
    Charset charset = Charset.forName(charsetName);
    return data.getBytes(charset);
  }


  public static byte[] getBytes(String data)
  {
    return getBytes(data, "GBK");
  }


  
  public static short getShort(byte[] bytes)
  {
    return (short) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)));
  }


  public static char getChar(byte[] bytes)
  {
    return (char) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)));
  }


  public static int getInt(byte[] bytes)
  {
    return (0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)) | (0xff0000 & (bytes[2] << 16)) | (0xff000000 & (bytes[3] << 24));
  }
  
  public static long getLong(byte[] bytes)
  {
    return(0xffL & (long)bytes[0]) | (0xff00L & ((long)bytes[1] << 8)) | (0xff0000L & ((long)bytes[2] << 16)) | (0xff000000L & ((long)bytes[3] << 24))
     | (0xff00000000L & ((long)bytes[4] << 32)) | (0xff0000000000L & ((long)bytes[5] << 40)) | (0xff000000000000L & ((long)bytes[6] << 48)) | (0xff00000000000000L & ((long)bytes[7] << 56));
  }


  public static float getFloat(byte[] bytes)
  {
    return Float.intBitsToFloat(getInt(bytes));
  }


  public static double getDouble(byte[] bytes)
  {
    long l = getLong(bytes);
    System.out.println(l);
    return Double.longBitsToDouble(l);
  }


  public static String getString(byte[] bytes, String charsetName)
  {
    return new String(bytes, Charset.forName(charsetName));
  }


  public static String getString(byte[] bytes)
  {
    return getString(bytes, "GBK");
  }


  
  public static void main(String[] args)
  {
    short s = 122;
    int i = 122;
    long l = 1222222;


    char c = 'a';


    float f = 122.22f;
    double d = 122.22;


    String string = "我是好孩子";
    System.out.println(s);
    System.out.println(i);
    System.out.println(l);
    System.out.println(c);
    System.out.println(f);
    System.out.println(d);
    System.out.println(string);


    System.out.println("**************");


    System.out.println(getShort(getBytes(s)));
    System.out.println(getInt(getBytes(i)));
    System.out.println(getLong(getBytes(l)));
    System.out.println(getChar(getBytes(c)));
    System.out.println(getFloat(getBytes(f)));
    System.out.println(getDouble(getBytes(d)));
    System.out.println(getString(getBytes(string)));
  }
 
}

以上这篇Java基本类型与byte数组之间相互转换方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 详解Spring-bean的循环依赖以及解决方式

    详解Spring-bean的循环依赖以及解决方式

    这篇文章主要介绍了详解Spring-bean的循环依赖以及解决方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-09-09
  • java中stack(栈)的使用代码实例

    java中stack(栈)的使用代码实例

    这篇文章主要介绍了java中stack(栈)的使用代码实例,具有一定借鉴价值,需要的朋友可以参考下。
    2017-12-12
  • 关于aop切面 注解、参数如何获取

    关于aop切面 注解、参数如何获取

    这篇文章主要介绍了关于aop切面 注解、参数如何获取,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教。
    2022-01-01
  • 解决调用ftpClient.retrieveFileStream(String remoteFilePath)第二次读取为空问题

    解决调用ftpClient.retrieveFileStream(String remoteFilePath)第二次读

    这篇文章主要给大家介绍了关于如何解决调用ftpClient.retrieveFileStream(String remoteFilePath)第二次读取为空问题的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2023-08-08
  • Java处理日期时间的方法汇总

    Java处理日期时间的方法汇总

    这篇文章主要给大家介绍了利用Java中的Calendar 类处理日期时间的方法汇总,其中包括取日期的每部分、取当月的第一天或最后一天、求两个日期之间相隔的天数以及一年前的日期等等的示例代码,有需要的朋友们可以直接参考借鉴,下面来一起看看吧。
    2016-12-12
  • 关于Spring中@Value注解使用和源码分析

    关于Spring中@Value注解使用和源码分析

    通过深入分析@Value注解的使用和源码,本文详细解释了Spring如何解析@Value注解并为属性赋值,首先,Spring会解析并收集所有被@Value注解修饰的属性,这一过程依赖于AutowiredAnnotationBeanPostProcessor类
    2024-11-11
  • java网络编程学习java聊天程序代码分享

    java网络编程学习java聊天程序代码分享

    java聊天程序代码分享,大家参考使用吧
    2013-12-12
  • JSON反序列化Long变Integer或Double的问题及解决

    JSON反序列化Long变Integer或Double的问题及解决

    这篇文章主要介绍了JSON反序列化Long变Integer或Double的问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • Spring框架中部署log4j.xml的详细步骤

    Spring框架中部署log4j.xml的详细步骤

    Log4j是一个常用的日志记录工具,它可以帮助我们记录应用程序的运行日志并进行灵活的配置,在Spring框架中,我们可以很方便地部署log4j.xml配置文件来管理日志记录,这篇文章主要介绍了Spring框架中部署log4j.xml的详细步骤并提供相应的代码示例,需要的朋友可以参考下
    2023-09-09
  • Springboot+MyBatist实现前后台交互登陆功能方式

    Springboot+MyBatist实现前后台交互登陆功能方式

    这篇文章主要介绍了Springboot+MyBatist实现前后台交互登陆功能方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01

最新评论