Java--byte and base type conversions

Source: Internet
Author: User

byteB[]={0x41, (byte) 0xc8,0,0}; //Method 1 Stream input, suitable for me/se environment//default big-endian number, if the small end number, you can first flip the arrayDataInputStream dis=NewDataInputStream (NewBytearrayinputstream (b));floatf=dis.readfloat ();d is.close (); System.out.println (f);//Method 2 Cache input for the SE/EE environmentBytebuffer Buf=bytebuffer.allocatedirect (4);//Direct cache with no additional memory//Buf=buf.order (Byteorder.little_endian);//default big-endian, small end with this lineBuf.put (b); Buf.rewind ();floatF2=buf.getfloat (); SYSTEM.OUT.PRINTLN (F2);
//method Three, custom tool class PackageCn.yuehua.io;/*** byte[] conversion * Using big-endian order, i.e. for 0x11223344,byte[0] save 0x11,byte[1] Save 0x22,byte[2] Save 0x33,byte[3] Save 0x44*/ Public classBytearrayconveter {//Char-byte[2]     Public Static byte[] Getbytearray (Charc) {        byte[] B =New byte[2]; b[0] = (byte) ((C & 0xff00) >> 8); b[1] = (byte) (C & 0X00FF); returnb; }    //gets a char from the index of a byte array at two consecutive bytes     Public Static CharGetChar (byte[] arr,intindex) {        return(Char) (0xff00 & Arr[index] << 8 | (0xFF & Arr[index + 1])); }    //Short converted to byte[2] array     Public Static byte[] Getbytearray ( Shorts) {byte[] B =New byte[2]; b[0] = (byte) ((S & 0xff00) >> 8); b[1] = (byte) (S & 0x00ff); returnb; }    //gets a short from two consecutive bytes at index of the byte array     Public Static  ShortGetshort (byte[] arr,intindex) {        return( Short) (0xff00 & Arr[index] << 8 | (0xFF & Arr[index + 1])); }    //int converted to byte[4] array     Public Static byte[] Getbytearray (inti) {byte[] B =New byte[4]; b[0] = (byte) ((I & 0xff000000) >> 24); b[1] = (byte) ((I & 0x00ff0000) >> 16); b[2] = (byte) ((I & 0x0000ff00) >> 8); b[3] = (byte) (I & 0X000000FF); returnb; }    //gets an int from 4 consecutive bytes at index of the byte array     Public Static intGetInt (byte[] arr,intindex) {        return(0xFF000000 & (Arr[index+0] << 24)) |                (0x00ff0000 & (Arr[index+1] << 16)) |                (0X0000FF00 & (Arr[index+2] << 8)) |                (0X000000FF & Arr[index+3]); }    //float converted to byte[4] array     Public Static byte[] Getbytearray (floatf) {intIntbits = Float.floattointbits (f);//interprets binary strings in float as int integers        returnGetbytearray (intbits); }    //gets a float from 4 consecutive bytes at index of the byte array     Public Static floatGetFloat (byte[] arr,intindex) {        returnfloat.intbitstofloat (arr, index) (GETINT); }    //long converted to byte[8] array     Public Static byte[] Getbytearray (Longl) {byteB[] =New byte[8]; b[0] = (byte) (0xFF & (L >> 56)); b[1] = (byte) (0xFF & (L >> 48)); b[2] = (byte) (0xFF & (L >> 40)); b[3] = (byte) (0xFF & (L >> 32)); b[4] = (byte) (0xFF & (L >> 24)); b[5] = (byte) (0xFF & (L >> 16)); b[6] = (byte) (0xFF & (L >> 8)); b[7] = (byte) (0xFF &l); returnb; }    //gets a long from 8 consecutive bytes from the index of the byte array     Public Static LongGetlong (byte[] arr,intindex) {        return(0xff00000000000000l & (Long) arr[index+0] << 56)) |                (0X00FF000000000000L & (Long) arr[index+1] << 48)) |                (0X0000FF0000000000L & (Long) arr[index+2] << 40)) |                (0X000000FF00000000L & (Long) arr[index+3] << 32)) |                (0X00000000FF000000L & (Long) Arr[index+4] << 24)) |                (0X0000000000FF0000L & (Long) arr[index+5] << 16)) |                (0X000000000000FF00L & (Long) Arr[index+6] << 8)) |                (0X00000000000000FFL & (Long) arr[index+7]); }    //double convert to byte[8] array     Public Static byte[] Getbytearray (Doubled) {LongLongbits =Double.doubletolongbits (d); returnGetbytearray (longbits); }    //gets a double from the index of the byte array at 8 consecutive bytes     Public Static DoubleGetDouble (byte[] arr,intindex) {        returndouble.longbitstodouble (arr, index) (Getlong); }     Public Static voidMain (string[] args) {floatf = 11.9f; byte[] ByteArray =Getbytearray (f); floatAfloat = getfloat (ByteArray, 0);    System.out.println (afloat); }}

Java--byte and base type conversions

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.