Several data types and byte conversion methods

Source: Internet
Author: User

[Java, double to byte]

public byte[] doubleToByte(double d) throws IOException {   double l = d ;   ByteArrayOutputStream baos = new ByteArrayOutputStream() ;   DataOutputStream dos = new DataOutputStream(baos) ;   dos.writeDouble(l) ;   byte b[] = baos.toByteArray() ;   return b ;}

[Java, byte [] to double]

public static double byteToDouble(byte[] b){     long l;    l=b[0];     l&=0xff;     l|=((long)b[1]<<8);     l&=0xffff;     l|=((long)b[2]<<16);     l&=0xffffff;     l|=((long)b[3]<<24);     l&=0xffffffffl;     l|=((long)b[4]<<32);     l&=0xffffffffffl;    l|=((long)b[5]<<40);     l&=0xffffffffffffl;     l|=((long)b[6]<<48);     l&=0xffffffffffffffl;     l|=((long)b[7]<<56);     return Double.longBitsToDouble(l); }

[Java, int to byte]

public byte[] intToByte(int i) {        byte[] abyte0 = new byte[4];        abyte0[0] = (byte) (0xff & i);        abyte0[1] = (byte) ((0xff00 & i) >> 8);        abyte0[2] = (byte) ((0xff0000 & i) >> 16);        abyte0[3] = (byte) ((0xff000000 & i) >> 24);        return abyte0;    }

[Java, byte to int]

public  static int bytesToInt(byte[] bytes) {        int addr = bytes[0] & 0xFF;        addr |= ((bytes[1] << 8) & 0xFF00);        addr |= ((bytes[2] << 16) & 0xFF0000);        addr |= ((bytes[3] << 24) & 0xFF000000);        return addr;    }

[Java, char to byte]

Public static byte [] charToByte (char ch) {int temp = (int) ch; byte [] B = new byte [2]; for (int I = B. length-1; I>-1; I --) {B = new Integer (temp & 0xff ). byteValue (); // Save the highest bit in the lowest Bit temp = temp> 8; // shift to the right eight places} return B ;}

[Conversion from java to byte to char]

public static char byteToChar(byte[] b){     int s=0;     if(b[0]>0)       s+=b[0];     else       s+=256+b[0];     s*=256;     if(b[1]>0)       s+=b[1];     else       s+=256+b[1];     char ch=(char)s;     return ch; }

[Java, byte to float]

public static float byteToFloat(byte[] v){        ByteBuffer bb = ByteBuffer.wrap(v);        FloatBuffer fb = bb.asFloatBuffer();        return fb.get();}

[Java, float to byte]

public static byte[] floatToByte(float v) {        ByteBuffer bb = ByteBuffer.allocate(4);        byte[] ret = new byte [4];        FloatBuffer fb = bb.asFloatBuffer();        fb.put(v);        bb.get(ret);        return ret;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.