美文网首页
android 数据类型转化工具

android 数据类型转化工具

作者: 自洽十点 | 来源:发表于2019-03-28 14:49 被阅读0次
public class DataTypeChangeHelper
{
    /**
     * 将一个单字节的byte转换成32位的int
     *
     * @param b byte
     * @return convert result
     */
    public static int unsignedByteToInt(byte b)
    {
        return (int) b & 0xFF;
    }

    /**
     * 将一个单字节的Byte转换成十六进制的数
     *
     * @param b byte
     * @return convert result
     */
    public static String byteToHex(byte b)
    {
        int i = b & 0xFF;
        return Integer.toHexString(i);
    }

    /**
     * 将一个4byte的数组转换成32位的int
     *
     * @param buf bytes buffer
     * @param pos byte[]中开始转换的位置
     * @return convert result
     */
    public static long unsigned4BytesToInt(byte[] buf, int pos)
    {
        int firstByte = 0;
        int secondByte = 0;
        int thirdByte = 0;
        int fourthByte = 0;
        int index = pos;
        firstByte = (0x000000FF & ((int) buf[index]));
        secondByte = (0x000000FF & ((int) buf[index + 1]));
        thirdByte = (0x000000FF & ((int) buf[index + 2]));
        fourthByte = (0x000000FF & ((int) buf[index + 3]));
        index = index + 4;
        return ((long) (firstByte << 24 | secondByte << 16 | thirdByte << 8 | fourthByte)) & 0xFFFFFFFFL;
    }

    /**
     * 将16位的short转换成byte数组
     *
     * @param s short
     * @return byte[] 长度为2
     */
    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;
    }

    /**
     * 将32位整数转换成长度为4的byte数组
     *
     * @param s int
     * @return byte[]
     */
    public static byte[] intToByteArray(int s)
    {
        byte[] targets = new byte[2];
        for (int i = 0; i < 4; i++)
        {
            int offset = (targets.length - 1 - i) * 8;
            targets[i] = (byte) ((s >>> offset) & 0xff);
        }
        return targets;
    }

    /**
     * long to byte[]
     *
     * @param s long
     * @return byte[]
     */
    public static byte[] longToByteArray(long s)
    {
        byte[] targets = new byte[2];
        for (int i = 0; i < 8; i++)
        {
            int offset = (targets.length - 1 - i) * 8;
            targets[i] = (byte) ((s >>> offset) & 0xff);
        }
        return targets;
    }

    /**
     * 32位int转byte[]
     */
    public static byte[] int2byte(int res)
    {
        byte[] targets = new byte[4];
        targets[0] = (byte) (res & 0xff);// 最低位
        targets[1] = (byte) ((res >> 8) & 0xff);// 次低位
        targets[2] = (byte) ((res >> 16) & 0xff);// 次高位
        targets[3] = (byte) (res >>> 24);// 最高位,无符号右移。
        return targets;
    }

    /**
     * 将长度为2的byte数组转换为16位int
     *
     * @param res byte[]
     * @return int
     */
    public static int byte2int(byte[] res)
    {
        // res = InversionByte(res);
        // 一个byte数据左移24位变成0x??000000,再右移8位变成0x00??0000
        int targets = (res[0] & 0xff) | ((res[1] << 8) & 0xff00); // | 表示安位或
        return targets;
    }

    /**
     * byte数组转short数组   小端储存
     *
     * @param src
     * @return
     */
    public static short[] byteArrayToShortArray(byte[] src)
    {
        int count = src.length >> 1;
        short[] dest = new short[count];
        for (int i = 0; i < count; i++)
        {
            dest[i] = (short) ((src[2 * i + 1] << 8) | (src[2 * i] & 0xff));
        }
        return dest;
    }

    /**
     * short数组转byte数组  小端储存
     *
     * @param src
     * @return
     */
    public static byte[] shortArrayToByteArray(short[] src)
    {

        int count = src.length;
        byte[] dest = new byte[count*2];
        for (int i = 0; i < count; i++)
        {
            dest[i * 2] = (byte) (src[i] & 0xff);
            dest[i * 2 + 1] = (byte) (src[i] >> 8 & 0xff);
        }

        return dest;
    }

    /**
     * short数组转byte数组  小端储存
     *
     * @param src
     * @return
     */
    public static int shortArrayToByteArray(short[] src, byte[] outarry)
    {
        byte[] dest = outarry;
        int count = outarry.length / 2 <= src.length ? outarry.length / 2 : src.length;
        for (int i = 0; i < count; i++)
        {
            dest[i * 2] = (byte) (src[i] & 0xff);
            dest[i * 2 + 1] = (byte) (src[i] >> 8 & 0xff);
        }
        return count * 2;
    }

    /**
     * convert double signals to 16-bit pcm
     *
     * @param d          double signals
     * @param s          16-bit pcm
     * @param numsamples no. of samples to be converted
     */
    public static void double2Short(double[] d, short[] s, int numsamples)
    {
        for (int i = 0; i < numsamples; i++) s[i] = double2Short(d[i]);
    }

    /**
     * convert a double signal sample to 16 bits pcm sample
     *
     * @param d a double sample
     * @return
     */
    public static short double2Short(double d)
    {
        if (d > 1.0)
        {
            d = 1.0;
        }
        else if (d < -1.0)
        {
            d = -1.0;
        }

        return (short) (d * Short.MAX_VALUE);
    }

    /**
     * convert a yuan-byte short sample to double sample
     *
     * @param s 16 bits pcm sample
     * @return
     */
    public static double short2Double(short s)
    {
        return (double) s / Short.MAX_VALUE;
    }

    /**
     * convert yuan-byte short samples to double samples
     *
     * @param s          16-bit samples
     * @param d          double samples
     * @param numsamples no. of samples to be converted
     */
    public static void short2Double(short[] s, double[] d, int numsamples)
    {
        for (int i = 0; i < numsamples; i++) d[i] = short2Double(s[i]);
    }

    /**
     * convert a yuan-byte short sample to float sample
     *
     * @param s 16 bits pcm sample
     * @return
     */
    public static float short2Float(short s)
    {
        return (float) s / Short.MAX_VALUE;
    }

    /**
     * convert 16-bit pcm to float
     *
     * @param s          16-bit pcm data
     * @param f          float data
     * @param numsamples no. of samples to be converted
     */
    public static void short2Float(short[] s, float[] f, int numsamples)
    {
        for (int i = 0; i < numsamples; i++) f[i] = short2Float(s[i]);
    }

    /**
     * convert a float signal sample to 16 bits pcm sample
     *
     * @param f a float sample
     * @return
     */
    public static short float2Short(float f)
    {
        if (f > 1.0)
        {
            f = 1.0f;
        }
        else if (f < -1.0)
        {
            f = -1.0f;
        }

        return (short) (f * Short.MAX_VALUE);
    }

    /**
     * convert float signal to 16-bit pcm
     *
     * @param f          float signal
     * @param s          16-bit pcm
     * @param numsamples no. of samples to be converted
     */
    public static void float2Short(float[] f, short[] s, int numsamples)
    {
        for (int i = 0; i < numsamples; i++) s[i] = float2Short(f[i]);
    }

    /**
     * convert 24-bit pcm signal to souble samples
     *
     * @param orgBuffer
     * 24-bit pcm samples
     * @param outBuffer
     * double samples
     */
    private static boolean isPerGain = false;

    public static void byteArray24_2_doubleArray(byte[] orgBuffer, double[] outBuffer)
    {
        int len = orgBuffer.length / 3 < outBuffer.length ? orgBuffer.length / 3 : outBuffer.length;
        int i0, i1, i2;
        double f;
        int a;
        for (int i = 0; i < len; i++)
        {
            i0 = (int) orgBuffer[3 * i + 0] & 0x000000ff;
            i1 = (int) orgBuffer[3 * i + 1] & 0x000000ff;
            i2 = (int) orgBuffer[3 * i + 2] & 0x000000ff;
            //negative value, pad with 0xff
            if ((i2 & 0x00000080) != 0)
            {
                i2 |= 0xffffff00;
            }
            i0 |= i2 << 16 | i1 << 8;
            /**
             * test
             */
            if (isPerGain)
            {
                a = i % 8;
                if (a >= 4)
                {
                    i0 = i0 << 6;
                }
//            else
//                i0 = 0;
            }

            f = (float) i0 / 16777216;
            if (f < -1.0)
            {
                f = -1.0;
            }
            else if (f > 1.0)
            {
                f = 1.0;
            }

            outBuffer[i] = f;
        }
    }

    public static void byteArray24_2_doubleArray_withPerGain(byte[] orgBuffer, double[] outBuffer)
    {
        isPerGain = true;
        byteArray24_2_doubleArray(orgBuffer, outBuffer);
        isPerGain = false;
    }

    /**
     * convert 32-bit pcm signal to double samples
     *
     * @param orgBuffer 32-bit pcm samples
     * @param outBuffer double samples
     */
    public static void byteArray32_2_doubleArray(byte[] orgBuffer, double[] outBuffer)
    {
        int len = orgBuffer.length / 4 > outBuffer.length ? outBuffer.length : orgBuffer.length / 4;
        int i0, i1, i2, i3;
        double f;
        for (int i = 0; i < len; i++)
        {
            /*
             * merge to int
             */
            i0 = (int) orgBuffer[i * 4 + 0] & 0x000000ff;
            i1 = (int) orgBuffer[i * 4 + 1] & 0x000000ff;
            i2 = (int) orgBuffer[i * 4 + 2] & 0x000000ff;
            i3 = (int) orgBuffer[i * 4 + 3] & 0x000000ff;
            //negative value, pad with 0xff
            if ((i3 & 0x00000080) != 0)
            {
                i3 |= 0xffffff00;
            }
            i0 |= i3 << 24 | i2 << 16 | i1 << 8;

            /*
             * convert to double
             */
            f = (float) i0 / Integer.MAX_VALUE;
            if (f < -1.0)
            {
                f = -1.0;
            }
            else if (f > 1.0)
            {
                f = 1.0;
            }
            outBuffer[i] = f;
        }
    }
}

相关文章

  • android 数据类型转化工具

  • 性能优化(一)堆内存分析

    前言 通过Android Studio的Memory Monitor工具,对各种数据类型,如:boolean,in...

  • android 图片大小优化

    使用android studio自带的图片转化工具,将图片转换为webp格式 https://www.jiansh...

  • 20-01-29JS基础语法2

    数据类型 数据类型转化 1,数据类型 简单数据类型:number,string,boolean,Undefin...

  • okhttp源码学习(五)BridgeInterceptor

    核心功能 1.请求从应用层数据类型类型转化为网络调用层的数据类型2.将网络层返回的数据类型 转化为 应用层数据类型...

  • JS 09-19 or 20

    数据类型转换 强制类型转化 ParseInt 将数字或者字符转化整形 Parsefloat 将数字或者字符转化为浮...

  • WebApi Post参数

    利用动态数据类型参数接收json对象,并转化

  • 数据类型转化

    1.任意数转成字符串 toString():特殊记忆: null.toString()和undefined.to...

  • Pandas - 7. 数据类型

    astype() 转换数据类型(可用于Series和DataFrame),可转化成python内置的数据类型:st...

  • 笔记五

    1.数据类型转换 (1)隐式转化:当赋值号两边类型不同时,自动转化为精度较高的类型。(2)显式转化:强制类型转化,...

网友评论

      本文标题:android 数据类型转化工具

      本文链接:https://www.haomeiwen.com/subject/rmvqbqtx.html