Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    public static final int FORMAT_SFLOAT = 50;
    public static final int FORMAT_FLOAT = 52;

    public static Float getFloatValue(byte[] value, int format, int position) {
        if (value == null)
            return null;
        if (position + (format & 0xF) > value.length)
            return null;
        int i;
        int mantissa;
        int exponent;
        switch (format) {
        case FORMAT_SFLOAT:
            i = value[(position + 1)];
            position = value[position];
            mantissa = signed((position & 0xFF) + ((i & 0xFF & 0xF) << 8), 12);
            exponent = signed((i & 0xFF) >> 4, 4);
            return Float.valueOf((float) (mantissa * Math.pow(10.0D, exponent)));
        case FORMAT_FLOAT:
            exponent = value[(position + 3)];
            mantissa = value[(position + 2)];
            i = value[(position + 1)];
            position = value[position];
            return Float.valueOf(
                    (float) ((format = signed((position & 0xFF) + ((i & 0xFF) << 8) + ((mantissa & 0xFF) << 16),
                            24)) * Math.pow(10.0D, exponent)));
        }
        return null;
    }

    private static int signed(int value, int length) {
        if ((value & 1 << length - 1) != 0)
            value = -1 * ((1 << length - 1) - (value & (1 << length - 1) - 1));
        return value;
    }
}