Java Unsigned Number Create unsignedInt(byte b)

Here you can find the source of unsignedInt(byte b)

Description

Get the specified byte's value as an unsigned integer.

License

Open Source License

Parameter

Parameter Description
b the byte to convert.

Return

An unsigned int representing the specified byte.

Declaration

public static int unsignedInt(byte b) 

Method Source Code

//package com.java2s;
/*//  ww  w  .j a  va 2s .  c om
 * Copyright (C) 2014 Jesse Caulfield 
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Get the specified byte's value as an unsigned integer.
     * <p>
     * This converts the specified byte into an integer. The least significant
     * byte (8 bits) of the integer will be identical to the byte (8 bits)
     * provided, and the most significant 3 bytes (24 bits) of the integer will be
     * zero.
     * <p>
     * For many of the values in this USB API, unsigned bytes are used. However,
     * since Java does not include unsigned bytes in the language, those unsigned
     * bytes must be converted to a larger storage type before being used in
     * unsigned calculations.
     *
     * @param b the byte to convert.
     * @return An unsigned int representing the specified byte.
     */
    public static int unsignedInt(byte b) {
        return 0x000000ff & b;
    }

    /**
     * Get the specified short's value as an unsigned integer.
     * <p>
     * This converts the specified byte into an integer. The least significant
     * short (16 bits) of the integer will be identical to the short (16 bits)
     * provided, and the most significant 2 bytes (16 bits) of the integer will be
     * zero.
     * <p>
     * For many of the values in this USB API, unsigned shorts are used. However,
     * since Java does not include unsigned short in the language, those unsigned
     * shorts must be converted to a larger storage type before being used in
     * unsigned calculations.
     *
     * @param s the short to convert.
     * @return An unsigned int representing the specified short.
     */
    public static int unsignedInt(short s) {
        return 0x0000ffff & s;
    }
}

Related

  1. unsignedCompare(long left, long right)
  2. unsignedConstraintValue(double v, double min, double max)
  3. unsignedDiv(long l1, long l2)
  4. unsignedExtend(String hex)
  5. unsignedHalfwordToString(int value)
  6. unsignedInt(int i)
  7. unsignedInt(int value)
  8. unsignedInt2ByteLE(byte[] bytes, long value, int offset)
  9. unsignedInt2Long(int x)