Example usage for java.lang Byte toUnsignedInt

List of usage examples for java.lang Byte toUnsignedInt

Introduction

In this page you can find the example usage for java.lang Byte toUnsignedInt.

Prototype

public static int toUnsignedInt(byte x) 

Source Link

Document

Converts the argument to an int by an unsigned conversion.

Usage

From source file:Main.java

public static void main(String[] args) {
    byte b = -10;
    int x = Byte.toUnsignedInt(b);
    System.out.println("Signed value in byte   = " + b);
    System.out.println("Unsigned value in  byte   = " + x);

}

From source file:Main.java

public static void main(String... args) {
    byte b = 2;//from   w  ww .jav  a  2 s.  co  m
    System.out.println(Byte.toUnsignedInt(b));

    b = -2;
    System.out.println(Byte.toUnsignedInt(b));
}

From source file:com.linkedin.pinot.common.utils.primitive.ByteArray.java

/**
 * Compares two byte[] values. The comparison performed is on unsigned value for each byte.
 * Returns:/*w  w w  .  j  ava  2s. co m*/
 * <ul>
 *   <li> 0 if both values are identical. </li>
 *   <li> -ve integer if first value is smaller than the second. </li>
 *   <li> +ve integer if first value is larger than the second. </li>
 * </ul>
 *
 * @param bytes1 First byte[] to compare.
 * @param bytes2 Second byte[] to compare.
 * @return Result of comparison as stated above.
 */
public static int compare(byte[] bytes1, byte[] bytes2) {
    int len1 = bytes1.length;
    int len2 = bytes2.length;
    int lim = Math.min(len1, len2);

    for (int k = 0; k < lim; k++) {
        // Java byte is always signed, but we need to perform unsigned comparison.
        int ai = Byte.toUnsignedInt(bytes1[k]);
        int bi = Byte.toUnsignedInt(bytes2[k]);
        if (ai != bi) {
            return ai - bi;
        }
    }
    return len1 - len2;
}

From source file:examples.utils.CifarReader.java

public static List<double[]> rawDouble(InputStream is) {
    List<double[]> result = new ArrayList<>();
    int row = 1 + (IMAGE_WIDTH * IMAGE_HIGHT * IMAGE_DEPTH);
    try {//from   w  w w . ja  v a2s .  c  o m
        while (is.available() > 0) {
            byte[] arr = new byte[row];
            is.read(arr);
            result.add(
                    Arrays.stream(ArrayUtils.toObject(arr)).mapToDouble(b -> Byte.toUnsignedInt(b)).toArray());
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(is);
    }
    return result;
}

From source file:hd3gtv.tools.Hexview.java

public String getView() {
    StringBuilder sb = new StringBuilder();

    int pos_number_size = String.valueOf(size).length();
    char[] string_view = new char[COLS];

    long current_pos;
    int zeros_to_add;
    String hex_number;/*  w w  w. j  a va  2 s.  c  om*/
    int value;
    for (int pos = this.pos; pos < len; pos++) {
        if (pos % COLS == 0) {
            current_pos = offset + (pos - this.pos);

            zeros_to_add = pos_number_size - String.valueOf(current_pos).length();
            sb.append(StringUtils.repeat("0", zeros_to_add));
            sb.append(current_pos);
            sb.append(" ");
        }

        if (pos % COLS == 4 | pos % COLS == 12 | pos % COLS == 20 | pos % COLS == 28) {
            sb.append(".");
        } else if (pos % COLS == 8 | pos % COLS == 16 | pos % COLS == 24) {
            sb.append("|");
        } else {
            sb.append(" ");
        }

        value = Byte.toUnsignedInt(datas[pos]);
        hex_number = String.format("%x", value);
        if (hex_number.length() == 1) {
            sb.append("0");
        }
        sb.append(hex_number);

        if (value > 31 & value < 127) {
            string_view[pos % COLS] = (char) datas[pos];
        } else {
            string_view[pos % COLS] = '.';
        }

        if (pos % COLS == COLS - 1) {
            sb.append(" ");
            for (int pos_sv = 0; pos_sv < string_view.length; pos_sv++) {
                if (pos_sv % (COLS / 2) == 0) {
                    sb.append(" ");
                }
                sb.append(string_view[pos_sv]);
            }

            sb.append(LINESEPARATOR);
        }
    }

    if ((len % COLS) > 0) {
        int empty_bytes = COLS - (len % COLS);
        for (int pos = 0; pos < empty_bytes; pos++) {
            sb.append("   ");
        }
        sb.append(" ");
        for (int pos = 0; pos < (len % COLS); pos++) {
            if (pos % (COLS / 2) == 0) {
                sb.append(" ");
            }
            sb.append(string_view[pos]);
        }
    }

    return sb.toString();
}

From source file:jlg.jade.test.asterix.cat062.Cat062LargeSampleTest.java

@Test
@Description("Used only for printing byte information that can help with developing the tool")
@Ignore// w ww . ja v a 2  s. co m
public void with_one_packet_should_print_bytes() throws IOException {
    try (InputStream is = TestHelper.getFileInputStreamFromResource("final_frame_062_one_packet_sample2.FF")) {
        FinalFrameReader ffReader = new FinalFrameReader();
        while (is.available() > 0) {
            byte[] ffPayload = ffReader.read(is);
            if (ffPayload != null) {
                System.out.println("DATA BLOCK START");
                for (int i = 0; i < ffPayload.length; i++) {
                    BitSet bs = BitSet.valueOf(new byte[] { ffPayload[i] });
                    System.out.print("ORIGINAL [ ");
                    for (int j = 0; j < 8; j++) {
                        if (bs.get(j)) {
                            System.out.print(1 + " ");
                        } else {
                            System.out.print(0 + " ");
                        }
                    }
                    System.out.print("]");

                    System.out.print("   REVERSE [ ");
                    for (int j = 8; j > 0; j--) {
                        if (bs.get(j)) {
                            System.out.print(1 + " ");
                        } else {
                            System.out.print(0 + " ");
                        }
                    }
                    System.out.print("]   ");
                    int unsignedValue = Byte.toUnsignedInt(ffPayload[i]);

                    System.out.println(unsignedValue);

                    if (i == 0 || i == 2) {
                        System.out.println("----------------------------------------------------------------");
                    }
                }
            }
            System.out.println("DATA BLOCK END");
            System.out.println(System.lineSeparator());
        }
    }
}

From source file:co.rsk.peg.BridgeSerializationUtilsTest.java

private RLPList decodeList(byte[] bytes) {
    // First byte => length of list (n)
    // Subsequent n bytes => length of each of the n elements
    // Subsequent bytes => elements
    RLPList decoded = new RLPList();
    int size = Byte.toUnsignedInt(bytes[0]);
    int offset = size + 1;
    for (int i = 1; i <= size; i++) {
        int elementSize = Byte.toUnsignedInt(bytes[i]);
        byte[] element = Arrays.copyOfRange(bytes, offset, offset + elementSize);
        decoded.add(new RLPItem(element));
        offset += elementSize;//from   w  w w .j  av a  2 s.c o m
    }
    return decoded;
}

From source file:org.ballerinalang.bre.bvm.CPU.java

private static void execTypeConversionOpcodes(WorkerExecutionContext ctx, WorkerData sf, int opcode,
        int[] operands) {
    int i;/*from  ww  w  . ja v a2  s  . co m*/
    int j;
    int k;
    BRefType bRefType;
    String str;

    switch (opcode) {
    case InstructionCodes.I2F:
        i = operands[0];
        j = operands[1];
        sf.doubleRegs[j] = sf.longRegs[i];
        break;
    case InstructionCodes.I2S:
        i = operands[0];
        j = operands[1];
        sf.stringRegs[j] = Long.toString(sf.longRegs[i]);
        break;
    case InstructionCodes.I2B:
        i = operands[0];
        j = operands[1];
        sf.intRegs[j] = sf.longRegs[i] != 0 ? 1 : 0;
        break;
    case InstructionCodes.I2BI:
        i = operands[0];
        j = operands[1];
        if (isByteLiteral(sf.longRegs[i])) {
            sf.refRegs[j] = new BByte((byte) sf.longRegs[i]);
        } else {
            handleTypeConversionError(ctx, sf, j, TypeConstants.INT_TNAME, TypeConstants.BYTE_TNAME);
        }
        break;
    case InstructionCodes.BI2I:
        i = operands[0];
        j = operands[1];
        sf.longRegs[j] = Byte.toUnsignedInt((byte) sf.intRegs[i]);
        break;
    case InstructionCodes.F2I:
        i = operands[0];
        j = operands[1];
        sf.longRegs[j] = (long) sf.doubleRegs[i];
        break;
    case InstructionCodes.F2S:
        i = operands[0];
        j = operands[1];
        sf.stringRegs[j] = Double.toString(sf.doubleRegs[i]);
        break;
    case InstructionCodes.F2B:
        i = operands[0];
        j = operands[1];
        sf.intRegs[j] = sf.doubleRegs[i] != 0.0 ? 1 : 0;
        break;
    case InstructionCodes.S2I:
        i = operands[0];
        j = operands[1];

        str = sf.stringRegs[i];
        if (str == null) {
            handleTypeConversionError(ctx, sf, j, null, TypeConstants.INT_TNAME);
            break;
        }

        try {
            sf.refRegs[j] = new BInteger(Long.parseLong(str));
        } catch (NumberFormatException e) {
            handleTypeConversionError(ctx, sf, j, TypeConstants.STRING_TNAME, TypeConstants.INT_TNAME);
        }
        break;
    case InstructionCodes.S2F:
        i = operands[0];
        j = operands[1];

        str = sf.stringRegs[i];
        if (str == null) {
            handleTypeConversionError(ctx, sf, j, null, TypeConstants.FLOAT_TNAME);
            break;
        }

        try {
            sf.refRegs[j] = new BFloat(Double.parseDouble(str));
        } catch (NumberFormatException e) {
            handleTypeConversionError(ctx, sf, j, TypeConstants.STRING_TNAME, TypeConstants.FLOAT_TNAME);
        }
        break;
    case InstructionCodes.S2B:
        i = operands[0];
        j = operands[1];
        sf.intRegs[j] = Boolean.parseBoolean(sf.stringRegs[i]) ? 1 : 0;
        break;
    case InstructionCodes.B2I:
        i = operands[0];
        j = operands[1];
        sf.longRegs[j] = sf.intRegs[i];
        break;
    case InstructionCodes.B2F:
        i = operands[0];
        j = operands[1];
        sf.doubleRegs[j] = sf.intRegs[i];
        break;
    case InstructionCodes.B2S:
        i = operands[0];
        j = operands[1];
        sf.stringRegs[j] = sf.intRegs[i] == 1 ? "true" : "false";
        break;
    case InstructionCodes.DT2XML:
        i = operands[0];
        j = operands[1];

        bRefType = sf.refRegs[i];
        if (bRefType == null) {
            handleNullRefError(ctx);
            break;
        }

        try {
            sf.refRegs[j] = XMLUtils.tableToXML((BTable) bRefType, ctx.isInTransaction());
        } catch (Exception e) {
            sf.refRegs[j] = null;
            handleTypeConversionError(ctx, sf, j, TypeConstants.TABLE_TNAME, TypeConstants.XML_TNAME);
        }
        break;
    case InstructionCodes.DT2JSON:
        i = operands[0];
        j = operands[1];

        bRefType = sf.refRegs[i];
        if (bRefType == null) {
            handleNullRefError(ctx);
            break;
        }

        try {
            sf.refRegs[j] = JSONUtils.toJSON((BTable) bRefType, ctx.isInTransaction());
        } catch (Exception e) {
            handleTypeConversionError(ctx, sf, j, TypeConstants.TABLE_TNAME, TypeConstants.XML_TNAME);
        }
        break;
    case InstructionCodes.T2MAP:
        convertStructToMap(ctx, operands, sf);
        break;
    case InstructionCodes.T2JSON:
        convertStructToJSON(ctx, operands, sf);
        break;
    case InstructionCodes.MAP2JSON:
        convertMapToJSON(ctx, operands, sf);
        break;
    case InstructionCodes.JSON2MAP:
        convertJSONToMap(ctx, operands, sf);
        break;
    case InstructionCodes.MAP2T:
        convertMapToStruct(ctx, operands, sf);
        break;
    case InstructionCodes.JSON2T:
        convertJSONToStruct(ctx, operands, sf);
        break;
    case InstructionCodes.XMLATTRS2MAP:
        i = operands[0];
        j = operands[1];

        bRefType = sf.refRegs[i];
        if (bRefType == null) {
            sf.refRegs[j] = null;
            break;
        }

        sf.refRegs[j] = ((BXMLAttributes) sf.refRegs[i]).value();
        break;
    case InstructionCodes.S2XML:
        i = operands[0];
        j = operands[1];
        k = operands[2];

        str = sf.stringRegs[i];
        if (str == null) {
            sf.refRegs[j] = null;
            sf.refRegs[k] = null;
            break;
        }

        try {
            sf.refRegs[j] = XMLUtils.parse(str);
            sf.refRegs[k] = null;
        } catch (BallerinaException e) {
            sf.refRegs[j] = null;
            handleTypeConversionError(ctx, sf, k, e.getMessage());
        }
        break;
    case InstructionCodes.XML2S:
        i = operands[0];
        j = operands[1];
        sf.stringRegs[j] = sf.refRegs[i].stringValue();
        break;
    case InstructionCodes.ANY2SCONV:
        i = operands[0];
        j = operands[1];

        bRefType = sf.refRegs[i];
        if (bRefType == null) {
            sf.stringRegs[j] = STRING_NULL_VALUE;
        } else {
            sf.stringRegs[j] = bRefType.stringValue();
        }
        break;
    default:
        throw new UnsupportedOperationException();
    }
}

From source file:org.fcrepo.apix.integration.StreamingIT.java

/**
 * Coverts the supplied byte array to a String hexadecimal representation, starting with the most significant bit.
 *
 * @param digest a byte array containing a message digest
 * @return a hexadecimal string representation of the message digest
 *///  ww w .  j  a v  a 2 s. c o  m
private static String asHex(final byte[] digest) {
    final StringBuilder buf = new StringBuilder();
    for (int i = 0; i < digest.length; i++) {
        buf.append(String.format("%02x", Byte.toUnsignedInt(digest[i])));
    }

    return buf.toString();
}