Java Integer Convert To convertIntToUsignedBytes(int integer)

Here you can find the source of convertIntToUsignedBytes(int integer)

Description

converts positive or zero integer to bytes array

License

Open Source License

Parameter

Parameter Description
integer integer to conversion

Return

array of bytes after conversion

Declaration

public static byte[] convertIntToUsignedBytes(int integer) 

Method Source Code

//package com.java2s;
/*/*from   w ww.ja  v  a 2  s .  co m*/
 * Collab desktop - Software for shared drawing via internet in real-time
 * Copyright (C) 2012 Martin Indra <aktive@seznam.cz>
 *
 * This file is part of Collab desktop.
 *
 * Collab desktop is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Collab desktop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Collab desktop.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * converts positive or zero integer to bytes array
     *
     * @param integer integer to conversion
     * @return array of bytes after conversion
     *
     * @see "collab protocol documentation"
     */
    public static byte[] convertIntToUsignedBytes(int integer) {
        if (integer < 0) {
            throw new IllegalArgumentException("Integer must be positive or zero!");
        }
        return new byte[] { (byte) ((0xff000000 & integer) >>> 24), (byte) ((0x00ff0000 & integer) >>> 16),
                (byte) ((0x0000ff00 & integer) >> 8), (byte) (0x000000ff & integer) };
    }
}

Related

  1. convertIntToDouble(final int[] intValues)
  2. convertIntToDWord(final int value)
  3. convertIntToLong(Integer intId)
  4. convertInttoMultiByte(int val)
  5. convertIntToTwoChar(int n)
  6. fromInt(byte[] buffer, int pos, int i)
  7. fromInt(final int value, final int length)
  8. fromInt(int i)
  9. fromint(int i)