Byte[] The conversion between an array and an int

Source: Internet
Author: User

Here's a simple way to record the two conversions:

The first type:

1, int and byte[] conversion (similar to the byte Short,long type)

[Java]View PlainCopy
  1. /**
  2. * Converts an int value to a byte array of four bytes, this method applies to the order of (low in front, high in the rear). Compatible with Bytestoint ()
  3. * @param value
  4. * int value to convert
  5. * @return byte array
  6. */
  7. Public static byte[] Inttobytes ( int value)
  8. {
  9. byte[] src = new byte[4];
  10. src[3] = (byte) ((value>>) & 0xFF);
  11. src[2] = (byte) ((value>>) & 0xFF);
  12. src[1] = (byte) ((value>>8) & 0xFF);
  13. src[0] = (byte) (Value & 0xFF);
  14. return src;
  15. }
  16. /**  
  17. * Converts an int value to a byte array of four bytes, this method applies to the order (high in front, low in the back). Compatible with BytesToInt2 ()
  18. */
  19. Public static byte[] IntToBytes2 (int value)
  20. {
  21. byte[] src = new byte[4];
  22. src[0] = (byte) ((value>>) & 0xFF);
  23. src[1] = (byte) ((value>>) & 0xFF);
  24. src[2] = (byte) ((value>>8) &0xFF);
  25. src[3] = (byte) (Value & 0xFF);
  26. return src;
  27. }


byte[] Turn int

[Java]View PlainCopy
  1. /**
  2. * Byte array to take an int value, this method applies to (low in front, high in the back) order, and Inttobytes () supporting the use of
  3. *
  4. * @param src
  5. * Byte array
  6. * @param offset
  7. * Starting from the offset bit of the array
  8. * @return int value
  9. */
  10. Public static int bytestoint (byte[] src, int offset) {
  11. int value;
  12. Value = (int) ((Src[offset] & 0xFF)
  13. | ((src[offset+1] & 0xFF) <<8)
  14. | ((src[offset+2] & 0xFF) <<
  15. |  ((src[offset+3] & 0xFF) <<24));
  16. return value;
  17. }
  18. /**  
  19. * The byte array takes an int value, this method is applied to the order (low in the back, high in front). Compatible with IntToBytes2 ()
  20. */
  21. Public static int BytesToInt2 (byte[] src, int offset) {
  22. int value;
  23. Value = (int) (((Src[offset] & 0xFF) <<
  24. | ((src[offset+1] & 0xFF) <<
  25. | ((src[offset+2] & 0xFF) <<8)
  26. |  (src[offset+3] & 0xFF));
  27. return value;
  28. }


The second type:

1, int and byte[] conversion (similar to the byte Short,long type)

[Java]View PlainCopy
  1. /**  
  2. * Converts an int value to a byte array of four bytes, this method applies to the order of (low in front, high in the rear).
  3. * @param value
  4. * int value to convert
  5. * @return byte array
  6. */
  7. Public static byte[] Inttobytes (int value)
  8. {
  9. byte[] byte_src = new byte[4];
  10. byte_src[3] = (byte) ((Value & 0xff000000) >>24);
  11. byte_src[2] = (byte) ((Value & 0x00ff0000) >>16);
  12. byte_src[1] = (byte) ((Value & 0x0000FF00) >>8);
  13. byte_src[0] = (byte) ((Value & 0x000000ff));
  14. return BYTE_SRC;
  15. }


byte[] Turn int

[Java]View PlainCopy
    1. /**  
    2. * The byte array takes an int value, this method is applied to the order (low in front, high in the rear).
    3. *
    4. * @param ary
    5. * Byte array
    6. * @param offset
    7. * Starting from the offset bit of the array
    8. * @return int value
    9. */
    10. Public static int bytestoint (byte[] ary, int offset) {
    11. int value;
    12. Value = (int) ((ary[offset]&0xFF)
    13. | ((ary[offset+1]<<8) & 0xff00)
    14. | ((ary[offset+2]<<) & 0xFF0000)
    15. |  ((ary[offset+3]<<) & 0xff000000));
    16. return value;
    17. }

Byte[] The conversion between an array and an int

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.