zoukankan      html  css  js  c++  java
  • 数字转换工具MathUtils

    package yqw.java.util;

    /**
     * 数字转换工具
     */
    public class MathUtils {

        /**
         * short转byte
         */
        public static byte[] toBytes(short s) {
            return new byte[] { (byte) (s & 0x00FF), (byte) ((s & 0xFF00) >> 8) };
        }


       /**
         * getAvgEnergy for int[]
         *
         * @param voiceEnergy
         * @return
         */
        public static int getAvgEnergy(int[] voiceEnergy) {
            int tmpSum = 0;
            int voiceLen = voiceEnergy.length;
            for (int i = 0; i < voiceLen; i++) {
                tmpSum += Math.abs(voiceEnergy[i]);
            }
            return tmpSum / voiceLen;
        }

        /**
         * byte[] to int []
         *
         * @param byteData
         * @return
         */
        public static int[] convertToInt(byte[] byteData) {
            if (byteData == null || byteData.length == 0)
                return null;
            int[] data = new int[byteData.length / 2];
            for (int i = 0; i < byteData.length; i = i + 2) {
                data[i / 2] = arr2int(byteData[i], byteData[i + 1]);
            }
            return data;
        }

        /**
         * arr2int
         *
         * @param arr0
         * @param arr1
         * @return
         */
        public static int arr2int(byte arr0, byte arr1) {
            int iLow = arr0;
            int iHigh = arr1;
            // Merge high-order and low-order byte to form a 16-bit double value.
            short i = (short) ((iHigh << 8) | (0xFF & iLow));
            return (int) i;
        }

        /**
         * short转换至字节数组
         *
         * @param s
         * @return
         */
        public static byte[] shortToByteArray(short s) {
            byte[] targets = new byte[2];
            for (int i = 0; i < 2; i++) {
                int offset = (targets.length - 1 - i) * 8;
                targets[i] = (byte) ((s >>> offset) & 0xff);
            }
            return targets;
        }


        /**
         * unsigned short转byte
         */
        public static byte[] unsignedShortToBytes(int s) {
            return new byte[] { (byte) (s & 0x000000FF), (byte) ((s & 0x0000FF00) >> 8) };
        }

        /**
         * int转byte
         */
        public static byte[] toBytes(int s) {
            return new byte[] { (byte) (s & 0x000000FF), (byte) ((s & 0x0000FF00) >> 8), (byte) ((s & 0x00FF0000) >> 16),
                    (byte) ((s & 0xFF000000) >> 24) };
        }

        /**
         * byte转int
         */
        public static int toInt(byte[] b) {
            return b[0] & 0xff | (b[1] & 0xff) << 8 | (b[2] & 0xff) << 16 | (b[3] & 0xff << 24);
        }

        /**
         * byte转long
         */
        public static long toUnsignedInt(byte[] b) {
            return b[0] & 0xff | (b[1] & 0xff) << 8 | (b[2] & 0xff) << 16 | (b[3] << 24);
        }

        /**
         * byte转short
         */
        public static short toShort(byte[] b) {
            // return (short) (b[0] << 24 | (b[1] & 0xff) << 16) ;
            return (short) (((b[1] << 8) | b[0] & 0xff));
        }

        /**
         * byte转unsigned short
         */
        public static int toUnsignedShort(byte[] b) {
            return (b[0] << 24 | (b[1] & 0xff) << 16);
        }

        /**
         * Assume the long is used as unsigned int
         *
         * @param s
         * @return
         */
        public static byte[] unsignedIntToBytes(long s) {
            return new byte[] { (byte) (s & 0x00000000000000FF), (byte) ((s & 0x000000000000FF00) >> 8),
                    (byte) ((s & 0x0000000000FF0000) >> 16), (byte) ((s & 0x00000000FF000000) >> 24) };
        }

        /**
         * float转换byte
         *
         * @param x
         * @param index
         */
        public static byte[] putFloat(float x) {
            byte[] b = new byte[4];
            int l = Float.floatToIntBits(x);
            for (int i = 0; i < 4; i++) {
                b[i] = new Integer(l).byteValue();
                l = l >> 8;
            }
            return b;
        }

        /**
         * 通过byte数组取得float
         *
         * @param b
         * @return
         */
        public static float getFloat(byte[] b) {
            int l;
            l = b[0];
            l &= 0xff;
            l |= ((long) b[1] << 8);
            l &= 0xffff;
            l |= ((long) b[2] << 16);
            l &= 0xffffff;
            l |= ((long) b[3] << 24);
            return Float.intBitsToFloat(l);
        }

    }

  • 相关阅读:
    php 函数strpos()
    uploadfy api中文文档
    thinkphp + 美图秀秀api 实现图片裁切上传,带数据库
    mysql 操作用户权限
    window.location 小结)
    turn.js 图书翻页效果
    thinkphp 内置标签volist 控制换行
    js 数据类型转换
    quartz 2.2.1
    Mysql测试链接
  • 原文地址:https://www.cnblogs.com/yang75n/p/8412911.html
Copyright © 2011-2022 走看看