Java Integer to Byte intToByte(final int i)

Here you can find the source of intToByte(final int i)

Description

Convert an integer (0 to 255) into a byte (-128 to 127).

License

Apache License

Parameter

Parameter Description
i integer

Return

the byte

Declaration

public static byte intToByte(final int i) 

Method Source Code

//package com.java2s;
/*/*from w w  w  .ja  v a  2  s  .  co m*/
 * #%L
 * utils-commons
 * %%
 * Copyright (C) 2016 - 2018 Gilles Landel
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

public class Main {
    private static final int HEX_FF = 0xFF;
    private static final int PERCENT_MAX = 100;

    /**
     * Convert an integer (0 to 255) into a byte (-128 to 127).
     * 
     * @param i
     *            integer
     * @return the byte
     */
    public static byte intToByte(final int i) {
        int percent = (i * Byte.MAX_VALUE) / PERCENT_MAX;

        byte ret = (byte) (percent & HEX_FF);

        return ret;
    }
}

Related

  1. int2ubyte(int in)
  2. intAsByte(int value)
  3. intTo1Byte(int x)
  4. intTo4Byte(int i)
  5. intToByte(byte[] buf, int off, int value)
  6. intToByte(int i)
  7. IntToByte(int i)
  8. intToByte(int i)
  9. intToByte(int i)