In Java, byte arrays and long arrays are converted to each other.

Source: Internet
Author: User
In Java, byte arrays and long arrays are converted to each other.
The byteandlongarraytest code is as follows:
  1. Package com. array. test;
  2. Import java. Io. ioexception;
  3. /**
  4. * @ Version 1.0
  5. * @ Author wangwei
  6. * @ Created on 2008-10-17
  7. */
  8. Public class byteandlongarraytest {
  9. /**
  10. * Convert a long array to a byte array
  11. *
  12. * @ Param longarray
  13. * @ Return
  14. * @ Throws ioexception
  15. */
  16. Public static byte [] longtobyte (long [] longarray) throws ioexception {
  17. Byte [] bytearray = new byte [longarray. length * 8];
  18. For (INT I = 0; I <longarray. length; I ++)
  19. {
  20. Bytearray [0 + 8 * I] = (byte) (longarray [I]> 56 );
  21. Bytearray [1 + 8 * I] = (byte) (longarray [I]> 48 );
  22. Bytearray [2 + 8 * I] = (byte) (longarray [I]> 40 );
  23. Bytearray [3 + 8 * I] = (byte) (longarray [I]> 32 );
  24. Bytearray [4 + 8 * I] = (byte) (longarray [I]> 24 );
  25. Bytearray [5 + 8 * I] = (byte) (longarray [I]> 16 );
  26. Bytearray [6 + 8 * I] = (byte) (longarray [I]> 8 );
  27. Bytearray [7 + 8 * I] = (byte) (longarray [I]> 0 );
  28. }
  29. Return bytearray;
  30. }
  31. /**
  32. * Convert byte array to long array
  33. *
  34. * @ Param bytearray
  35. * @ Return
  36. * @ Throws ioexception
  37. */
  38. Public static long [] bytetolong (byte [] bytearray) throws ioexception {
  39. Long [] longarray = new long [bytearray. Length/8];
  40. For (INT I = 0; I <longarray. length; I ++)
  41. {
  42. Longarray [I] = (long) bytearray [0 + 8 * I] & 0xff) <56)
  43. | (Long) bytearray [1 + 8 * I] & 0xff) <48)
  44. | (Long) bytearray [2 + 8 * I] & 0xff) <40)
  45. | (Long) bytearray [3 + 8 * I] & 0xff) <32)
  46. | (Long) bytearray [4 + 8 * I] & 0xff) <24)
  47. | (Long) bytearray [5 + 8 * I] & 0xff) <16)
  48. | (Long) bytearray [6 + 8 * I] & 0xff) <8)
  49. | (Long) bytearray [7 + 8 * I] & 0xff) <0 );
  50. }
  51. Return longarray;
  52. }
  53. /**
  54. * @ Param ARGs
  55. */
  56. Public static void main (string [] ARGs) throws exception {
  57. // Todo auto-generated method stub
  58. Long [] longarray = {500000 };
  59. Byte [] bytearray = longtobyte (longarray );
  60. For (INT I = 0; I <bytearray. length; I ++)
  61. {
  62. System. Out. Print (bytearray [I] + "");
  63. }
  64. System. Out. println ();
  65. Long [] longarray2 = bytetolong (bytearray );
  66. For (INT I = 0; I <longarray. length; I ++)
  67. System. Out. Print (longarray [I] + "");
  68. }
  69. }

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.