集册 Java实例教程 读取一个十进制值作为IEEE

读取一个十进制值作为IEEE

欢马劈雪     最近更新时间:2020-01-02 10:19:05

520
从ByteBuffer读取IEEE-11073 32位浮点数时的十进制值

/***************************** BEGIN LICENSE BLOCK ***************************


The contents of this file are subject to the Mozilla Public License, v. 2.0.

If a copy of the MPL was not distributed with this file, You can obtain one

at http://mozilla.org/MPL/2.0/.


Software distributed under the License is distributed on an "AS IS" basis,

WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License

for the specific language governing rights and limitations under the License.

         

Copyright (C) 2012-2016 Sensia Software LLC. All Rights Reserved.

         

 ******************************* END LICENSE BLOCK ***************************/

//package com.nowjava;

import java.nio.ByteBuffer;/*时 代 J a v a - nowjava.com 提 供*/


public class Main {

    /** 

     * Reads a decimal value as a IEEE-11073 32-bits float

     * This is necessary because the default float format in java is IEEE-754

     * @param data byte buffer to read the 4 bytes from

     * @return java float value decoded from the given bytes

     **/

    public static float readHealthFloat32(ByteBuffer data) {

        byte b0 = data.get();

        byte b1 = data.get();

        byte b2 = data.get();

        byte b3 = data.get();

        int mantissa = unsignedToSigned((b0 & 0xFF) + ((b1 & 0xFF) << 8)

                + ((b2 & 0xFF) << 16), 24);// from N o w  J a v a  . c o m

        return (float) (mantissa * Math.pow(10, b3));

    }


    private static 
展开阅读全文