Android Byte Array to Int Convert getUnsignedSafe(byte[] buffer, int pos)

Here you can find the source of getUnsignedSafe(byte[] buffer, int pos)

Description

Convert byte from byte buffer to unsigned integer.

License

Open Source License

Parameter

Parameter Description
buffer a parameter
pos : index of the byte in the buffer.

Declaration

public static int getUnsignedSafe(byte[] buffer, int pos) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from   www .  ja v  a 2  s .co m*/
     * Convert byte from byte buffer to unsigned integer.
     * @param buffer
     * @param pos : index of the byte in the buffer.
     * @return
     */
    public static int getUnsignedSafe(byte[] buffer, int pos) {
        if (null == buffer || pos >= buffer.length || pos < 0) {
            return 0;
        }
        int res = toUnsignedInteger(buffer[pos]);
        return res;
    }

    /**
     * Convert byte to unsigned integer.
     * @param b
     * @return
     */
    public static int toUnsignedInteger(byte b) {
        int n = b >= 0 ? b : b + 256;
        return n;
    }
}

Related

  1. toInt(byte[] bytes)
  2. toInteger(byte[] input)
  3. toInteger(byte[] input, int offset)
  4. readInt(byte[] buff, int pos)
  5. getShort(byte[] data)
  6. toUInt16(byte[] bytes, int start)
  7. intToOctet(byte[] buf, int i)
  8. readInt(byte[] byteArray, int offset)
  9. writeInt(byte[] byteArray, int offset, int i)