最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java基本類型與byte數(shù)組之間相互轉(zhuǎn)換方法

 更新時(shí)間:2016年08月23日 10:16:17   投稿:jingxian  
下面小編就為大家?guī)硪黄狫ava基本類型與byte數(shù)組之間相互轉(zhuǎn)換方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

Java基本類型與byte數(shù)組之間相互轉(zhuǎn)換,剛剛寫的

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數(shù)組之間相互轉(zhuǎn)換方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解Spring-bean的循環(huán)依賴以及解決方式

    詳解Spring-bean的循環(huán)依賴以及解決方式

    這篇文章主要介紹了詳解Spring-bean的循環(huán)依賴以及解決方式,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-09-09
  • java中stack(棧)的使用代碼實(shí)例

    java中stack(棧)的使用代碼實(shí)例

    這篇文章主要介紹了java中stack(棧)的使用代碼實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • 關(guān)于aop切面 注解、參數(shù)如何獲取

    關(guān)于aop切面 注解、參數(shù)如何獲取

    這篇文章主要介紹了關(guān)于aop切面 注解、參數(shù)如何獲取,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
    2022-01-01
  • 解決調(diào)用ftpClient.retrieveFileStream(String?remoteFilePath)第二次讀取為空問題

    解決調(diào)用ftpClient.retrieveFileStream(String?remoteFilePath)第二次讀

    這篇文章主要給大家介紹了關(guān)于如何解決調(diào)用ftpClient.retrieveFileStream(String?remoteFilePath)第二次讀取為空問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-08-08
  • Java處理日期時(shí)間的方法匯總

    Java處理日期時(shí)間的方法匯總

    這篇文章主要給大家介紹了利用Java中的Calendar 類處理日期時(shí)間的方法匯總,其中包括取日期的每部分、取當(dāng)月的第一天或最后一天、求兩個(gè)日期之間相隔的天數(shù)以及一年前的日期等等的示例代碼,有需要的朋友們可以直接參考借鑒,下面來一起看看吧。
    2016-12-12
  • 關(guān)于Spring中@Value注解使用和源碼分析

    關(guān)于Spring中@Value注解使用和源碼分析

    通過深入分析@Value注解的使用和源碼,本文詳細(xì)解釋了Spring如何解析@Value注解并為屬性賦值,首先,Spring會解析并收集所有被@Value注解修飾的屬性,這一過程依賴于AutowiredAnnotationBeanPostProcessor類
    2024-11-11
  • java網(wǎng)絡(luò)編程學(xué)習(xí)java聊天程序代碼分享

    java網(wǎng)絡(luò)編程學(xué)習(xí)java聊天程序代碼分享

    java聊天程序代碼分享,大家參考使用吧
    2013-12-12
  • JSON反序列化Long變Integer或Double的問題及解決

    JSON反序列化Long變Integer或Double的問題及解決

    這篇文章主要介紹了JSON反序列化Long變Integer或Double的問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Spring框架中部署log4j.xml的詳細(xì)步驟

    Spring框架中部署log4j.xml的詳細(xì)步驟

    Log4j是一個(gè)常用的日志記錄工具,它可以幫助我們記錄應(yīng)用程序的運(yùn)行日志并進(jìn)行靈活的配置,在Spring框架中,我們可以很方便地部署log4j.xml配置文件來管理日志記錄,這篇文章主要介紹了Spring框架中部署log4j.xml的詳細(xì)步驟并提供相應(yīng)的代碼示例,需要的朋友可以參考下
    2023-09-09
  • Springboot+MyBatist實(shí)現(xiàn)前后臺交互登陸功能方式

    Springboot+MyBatist實(shí)現(xiàn)前后臺交互登陸功能方式

    這篇文章主要介紹了Springboot+MyBatist實(shí)現(xiàn)前后臺交互登陸功能方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01

最新評論

城口县| 大宁县| 新昌县| 花垣县| 新乡市| 富阳市| 喀喇沁旗| 如东县| 贡嘎县| 鹤壁市| 达拉特旗| 烟台市| 铁力市| 泸溪县| 赣州市| 凤凰县| 新和县| 毕节市| 镇雄县| 衡东县| 龙胜| 隆化县| 怀安县| 安福县| 盘山县| 白朗县| 溆浦县| 墨竹工卡县| 彰化市| 常州市| 广德县| 根河市| 南乐县| 加查县| 牟定县| 剑河县| 从化市| 珠海市| 安西县| 布尔津县| 连平县|