国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

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

2019-11-26 13:56:29
字體:
供稿:網(wǎng)友

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)容了,希望能給大家一個參考,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 连南| 齐齐哈尔市| 兰考县| 崇信县| 杂多县| 五台县| 东乌珠穆沁旗| 彝良县| 吕梁市| 项城市| 九龙城区| 海阳市| 宜丰县| 贺州市| 洛隆县| 类乌齐县| 烟台市| 杭锦后旗| 山西省| 会东县| 泗洪县| 定结县| 阿克陶县| 同江市| 梓潼县| 军事| 平潭县| 略阳县| 舒城县| 孟津县| 中牟县| 北流市| 汤原县| 凤山市| 广州市| 顺平县| 泰兴市| 新乡市| 无棣县| 桃园县| 廊坊市|