android 數據類型轉化工具

public class DataTypeChangeHelper
{
    /**
     * 將一個單字節(jié)的byte轉換成32位的int
     *
     * @param b byte
     * @return convert result
     */
    public static int unsignedByteToInt(byte b)
    {
        return (int) b & 0xFF;
    }

    /**
     * 將一個單字節(jié)的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;
        }
    }
}
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末般卑,一起剝皮案震驚了整個濱河市褐啡,隨后出現的幾起案子冠蒋,更是在濱河造成了極大的恐慌弹澎,老刑警劉巖撑瞧,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件袋励,死亡現場離奇詭異柒昏,居然都是意外死亡凳宙,警方通過查閱死者的電腦和手機,發(fā)現死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進店門职祷,熙熙樓的掌柜王于貴愁眉苦臉地迎上來氏涩,“玉大人,你說我怎么就攤上這事有梆∈羌猓” “怎么了?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵泥耀,是天一觀的道長饺汹。 經常有香客問我,道長痰催,這世上最難降的妖魔是什么兜辞? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮夸溶,結果婚禮上逸吵,老公的妹妹穿的比我還像新娘。我一直安慰自己蜘醋,他們只是感情好胁塞,可當我...
    茶點故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著压语,像睡著了一般啸罢。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上胎食,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天扰才,我揣著相機與錄音,去河邊找鬼厕怜。 笑死衩匣,一個胖子當著我的面吹牛,可吹牛的內容都是我干的粥航。 我是一名探鬼主播琅捏,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼递雀!你這毒婦竟也來了柄延?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤缀程,失蹤者是張志新(化名)和其女友劉穎搜吧,沒想到半個月后市俊,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡滤奈,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年摆昧,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蜒程。...
    茶點故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡绅你,死狀恐怖,靈堂內的尸體忽然破棺而出搞糕,到底是詐尸還是另有隱情勇吊,我是刑警寧澤,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布窍仰,位于F島的核電站,受9級特大地震影響礼殊,放射性物質發(fā)生泄漏驹吮。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一晶伦、第九天 我趴在偏房一處隱蔽的房頂上張望碟狞。 院中可真熱鬧,春花似錦婚陪、人聲如沸族沃。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽脆淹。三九已至,卻和暖如春沽一,著一層夾襖步出監(jiān)牢的瞬間盖溺,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工铣缠, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留烘嘱,地道東北人。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓蝗蛙,卻偏偏與公主長得像蝇庭,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子捡硅,可洞房花燭夜當晚...
    茶點故事閱讀 44,779評論 2 354

推薦閱讀更多精彩內容