byte-to-int transfer in Java

Source: Internet
Author: User

    • Package com.yl.common.utils;

    • /**

    • * Byte Conversion Tool

    • *

    • * @author HUANGZP

    • * @date 2015-6-09

    • */

    • Public

Class Byteutil {

    • /**

    • * Convert ISource to byte array of length Iarraylen, lower byte array is the low byte bit of integer type

    • * @param iSource

    • * @param Iarraylen

    • * @return

    • */

    • Public

Static byte[] Tobytearray (int iSource, int iarraylen) {

    • Byte

[] Blocalarr = new Byte[iarraylen];

    • For

(int i = 0; (I < 4) && (I < iarraylen); i++) {

    • Blocalarr[i] = (byte

) (ISource >> 8 * I & 0xFF);

    • }
    • Return

Blocalarr;

    • }
    • /**

    • * Converts a byte array Brefarr to an integer, and the lower byte array is the low byte bit of the integer type

    • * @param Brefarr

    • * @return

    • */

    • Public

static int ToInt (byte[] brefarr) {

    • Int

Ioutcome = 0;

    • Byte

Bloop;

    • For

(int i = 0; i < 4; i++) {

    • Bloop = Brefarr[i];
    • Ioutcome + = (Bloop & 0xFF

) << (8 * i);

    • }
    • Return

Ioutcome;

    • }
    • }
    • Package

Com.yl.common.utils;

    • /**

    • * Byte Conversion Tool

    • *

    • * @author HUANGZP

    • * @date 2015-6-09

    • */

    • Public

Class Byteutil {

    • /**

    • * Convert ISource to byte array of length Iarraylen, lower byte array is the low byte bit of integer type

    • * @param iSource

    • * @param Iarraylen

    • * @return

    • */

    • Public

Static byte[] Tobytearray (int iSource, int iarraylen) {

    • Byte

[] Blocalarr = new Byte[iarraylen];

    • For

(int i = 0; (I < 4) && (I < iarraylen); i++) {

    • Blocalarr[i] = (byte

) (ISource >> 8 * I & 0xFF);

    • }
    • Return

Blocalarr;

    • }
    • /**

    • * Converts a byte array Brefarr to an integer, and the lower byte array is the low byte bit of the integer type

    • * @param Brefarr

    • * @return

    • */

    • Public

static int ToInt (byte[] brefarr) {

    • Int

Ioutcome = 0;

    • Byte

Bloop;

    • For

(int i = 0; i < 4; i++) {

    • Bloop = Brefarr[i];
    • Ioutcome + = (Bloop & 0xFF

) << (8 * i);

    • }
    • Return

Ioutcome;

    • }
    • }  ?

Original Address http://techfoxbbs.com/blog-1-5.html

byte-to-int transfer in Java

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.