数字转换工具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);
    }

}

原文地址:https://www.cnblogs.com/yang75n/p/8412911.html

时间: 2024-08-07 15:41:51

数字转换工具MathUtils的相关文章

unicode 编码在线转换工具--javascript

unicode 编码在线转换工具--javascript 本人在网上搜索,看到有使用javascript做unicode编码转换的,感觉很好玩,所以拿来使用的. 这个功能有目前测试了两种: 1)unicode互相转换,例如\u4e2d\u56fd转换成字符,支持中文和英文: 2)URL编码的解析,例如众所周知的%20是表示空格,还有%24,%33,%44,%62等等: 我相信,只要你试试上面的字符进行解码,你就会知道他们之间关系,如何表示字符,数字,字母. 以上可以直接运行.下面给出相关的JS代

常用的Java字符转换工具类

该类里包含了Java中常用的字符串处理方法,如日期处理.金额转换... ----------------------------------------------------------------------------------------- import java.math.BigDecimal;import java.sql.Timestamp;import java.text.SimpleDateFormat;import java.util.Calendar;import jav

java 支持分词的高性能拼音转换工具,速度是 pinyin4j 的两倍

pinyin pinyin 是 java 实现的高性能中文拼音转换工具. 变更日志 创作目的 想为 java 设计一款便捷易用的拼音工具. 如何为 java 设计一款高性能的拼音转换工具 pinyin4j 特性 性能是 pinyin4j 的两倍 极简的 api 设计 支持转换长文本 支持多音字 支持多种拼音标注方式 支持中文分词 快速开始 准备 jdk 1.7+ maven 引入 <dependency> <groupId>com.github.houbb</groupId&

2020_1课程设计—基于BC的证书格式转换工具的设计与实现—Week1

2020_1课程设计-基于BC的证书格式转换工具的设计与实现-Week1 目录 2020_1课程设计-基于BC的证书格式转换工具的设计与实现-Week1 任务要求 Week1 任务安排 实践过程 学习证书格式的相关知识 学习OpenSSL的使用方法 安装OpenSSL 使用OpenSSL查看证书,并实现证书格式转换 创建根证书CA 颁发证书 证书格式转换 遇到问题 参考链接 任务要求 清楚.pem .pfx /.keystore .crt .cer .der 这些格式的文件用openssl如何产

在线转换工具

在线XML.JSON数据互转 http://www.bejson.com/go.php?u=http://www.bejson.com/xml2json Base64在线编码解码 http://tool.chinaz.com/Tools/Base64.aspx http://www1.tc711.com/tool/BASE64.htm http://tools.jb51.net/tools/base64_decode-gb2312.php base64特别适合在http,mime协议下快速传输数

SQLServer(MSSQL)、MySQL、SQLite、Access相互迁移转换工具 DB2DB v1.4

最近公司有一个项目,需要把原来的系统从 MSSQL 升迁到阿里云RDS(MySQL)上面.为便于测试,所以需要把原来系统的所有数据表以及测试数据转换到 MySQL 上面.在百度上找了很多方法,有通过微软 DTS 的,也有使用 mss2sql 工具进行转换的.使用 DTS 需要预先创建好数据表,否则新迁移的数据库是没有主键的.而 mss2sql 工具可以解决以上问题,但转换速度非常慢!我需要转换 3000 万的数据,在一台相当不错的服务器上面,也需要几天几夜才能转换完成.而 DB2DB 就是在这样

【分享】Android Studio专用文件转换工具:把ANSI文件批量另存为无BOM的UTF-8文件

[分享]Android Studio专用文件转换工具:把ANSI文件批量另存为无BOM的UTF-8文件 在Andoird Studio下编译java文件时,经常会出现像下面的错误: Error:(29, 43) 閿欒: 缂栫爜UTF-8鐨勪笉鍙槧灏勫瓧绗? 在这里,分享一个工具:ANSI文件批量另存为无BOM的UTF-8文件: 把下面代码用记事本存为AndroidStudioJava编码.vbs,双击即可使用: on error resume next Set WshShell=WScrip

在C#中将数字转换成中文

上篇我们讲了在MSSQL中将数字转换成中文,这篇我们讲讲在C#中将数字转换成中文 下篇将讲一下如何将金额转换成中文金额,废话不多说,具体代码如下: /// <summary> /// 数字转中文 /// </summary> /// <param name="number">eg: 22</param> /// <returns></returns> public string NumberToChinese(in

Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】

package com.soai.imdemo; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.grap