欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-13 13:02:33
java基本类型与byte数组之间相互转换,刚刚写的 package cn.teaey.utils; import java.nio.charset.c...

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数组之间相互转换方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。